• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » Android » Android Tutorial – Using AsyncTask for Android With Simple Example for Beginners

Android Tutorial – Using AsyncTask for Android With Simple Example for Beginners

By Sigit Prasetya Nugroho ∙ January 14, 2016 ∙ Android ∙ Leave a Comment

Share : TwitterFacebookTelegramWhatsapp

Seegatesite – I’ll share how to use AsyncTask for android. What does it AsyncTask? AsyncTask is one of the features in Android Programming for executing the operation to be performed in the background. So basically operations are performed in the background thread and then when it is finished it will be directly displayed in the UI. Either successful or unsuccessful in accordance with the pre-arranged. In the previous article about xml parser android, I use the AsyncTask method for which we will execute the xml file. How important AsyncTask for android application?

According to the Android Developers’s Site

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

In essence, the process AsyncTask background done in a separate thread.

Okay, for more details, we immediately began the following experiment

Table of Contents

  • 1 Creating a simple application using AsyncTask for android 
    • 1.1 Thus article about Android Tutorial – Using AsyncTask for Android With Simple Example for Beginners, hope useful.

Creating a simple application using AsyncTask for android 

1. Create a new android project named AsynctaskTutorial, read how to create new android project in android studio

2. Add TextView, Progress Bar, and Button on activity_main.xml

Android Tutorial Using Asynctask Android For Beginner

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="20dp"
android:text="Tutorial Using ASyncTask"
tools:context=".AsyncTaskActivity" />
 
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="34dp" />
 
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progress"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:minWidth="120dp"
android:text="Process" />
 
<TextView
android:id="@+id/txtpercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/progress"
android:text="Processing 0%"
android:textAppearance="?android:attr/textAppearanceMedium" />

3. Add the following script to MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.example.sigit.asynctasktutorial; // change with your package name
 
import android.os.AsyncTask;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
Button btnprocess;
ProgressBar progressBar;
TextView txtpercentage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnprocess = (Button) findViewById(R.id.button);
progressBar = (ProgressBar) findViewById(R.id.progressbar);
txtpercentage= (TextView) findViewById(R.id.txtpercentage);
 
btnprocess.setOnClickListener(new View.OnClickListener() {
 
@Override
public void onClick(View v) {
btnprocess.setEnabled(false);
new DoingAsyncTask().execute();
}
});
}
private class DoingAsyncTask extends AsyncTask<Void, Integer, Void> {
 
int progress_status;
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this,"Invoke onPreExecute() Process", Toast.LENGTH_SHORT).show();
progress_status = 0;
txtpercentage.setText("Processing 0%");
}
@Override
protected Void doInBackground(Void... params) {
while(progress_status<100){
progress_status += 5;
publishProgress(progress_status);
SystemClock.sleep(200);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
progressBar.setProgress(values[0]);
txtpercentage.setText("Processing " +values[0]+"%");
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this,"Invoke onPostExecute() Process", Toast.LENGTH_SHORT).show();
txtpercentage.setText("Processing complete");
btnprocess.setEnabled(true);
}
}
}

4. Run App And Click the Process Button

Android Tutorial Using Asynctask With Simple Example Android

Explanation :

1. onPreExecute()

in this function we prepare early initialization settings, before the operation is executed. In the example above, we initialize progress_status = 0 and display TextView with the word “Processing 0%”. After the process onPreExecute() is complete, the process is continued with doInBackground().

2. doInBackground()

The part where the operations are performed in the background. In the example above will be looping to add the value of progressbar. The value of the progress bar will be displayed to the UI with publishProgress’s method.

3. onProgressUpdate()

when the operating process runs every change will be displayed. onProgressUpdate () will capture the data that is given by the method publishProgress and shown to the UI. In the example above, OnProgressUpdate() will execute progressBar.setProgress(values[0]) and txtpercentage.setText(“Processing ” +values[0]+”%”)

4. onPostExecute()

after the process / operation is completed this function is executed

Thus article about Android Tutorial – Using AsyncTask for Android With Simple Example for Beginners, hope useful.

if you need more example how to use asynctask for android , please read my another article Android Tutorial – How to Use XML Parser Android with Simple Example for Beginner

Another Android Related Post :

  • Easily Download and Install Pokemon Go on Android in Unregistered Country
  • Tutorial How to Create Download File From Url On Ionic Framework
  • How to Create Android Image Gallery with Ionic Framework and Web Services
  • Ionic Framework – Introduction and Installation Tutorial on Windows and Ubuntu
  • Android Tutorial – Display Image To Android Gridview From Url Using Picasso
  • Android Tutorial – Step By Step Learning Android Sqlite Database for Beginner

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for Sigit Prasetya NugrohoThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (7) Publisher Tips (5) react (3) Reactjs (7) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) widget (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2021 Seegatesite.com