Android development: AsyncTask background asynchronous task implementation

In Android development, often have to use some network for long time operation, if the direct execution sequence, interface will be temporarily black and death card, so the user experience is not good, we can use the asynchronous task to implement these operations. With the new eclipse Activity, select the LoginActivity template, will automatically generate the asynchronous task template has the login operation.

We perform background tasks, pop up a schedule box ProgressDialog, giving users a better experience:

1 ProgressDialog progress = new ProgressDialog(this);
2 progress.setMessage("Please wait ...");

We want to perform background tasks inherit from the AsyncTask class to implement, and override the doInBackground, the onPostExecute two method, doInBackground method perform background tasks, onPostExecute method for task execution after treatment. The details of the AsyncTask class can refer to the official document: http://developer.android.com/reference/android/os/AsyncTask.htm.

 1 class LoginTask extends AsyncTask<Void, Void, Boolean> {
 2     
 3     @Override
 4     protected Boolean doInBackground(Void... args) {
 5         // To perform background tasks, login, query etc. . 
 6         return true;
 7     }
 8 
 9     @Override
10     public void onPostExecute(Boolean result) {
11         task = null;
12         progress.dismiss();
13         
14         if (result) {
15             Toast.makeText(LoginActivity.this, "Success .", Toast.LENGTH_LONG).show();
16         } else {
17             Toast.makeText(LoginActivity.this, "Failed !", Toast.LENGTH_LONG).show();
18         }
19     }
20 }

In AsyncTask, The first Void is the type of the parameter, If the mission when passing parameters, You can change the type of, The corresponding parameter's type doInBackground will be changed; second Void is the task execution schedule, According to this value control the progress of the tip; third Boolean is returned as a result of the background task execution, Here the use of Boolean type, According to the true/false judgment task returns whether the implementation is successful, If the parameters need to modify the onPostExecute method to modify the.

Add the task calls in the interface control's click event:

 1 button.setOnClickListener(new OnClickListener() {
 2     @Override
 3     public void onClick(View v) {
 4         if(task != null) {
 5             Toast.makeText(LoginActivity.this, "Task is running ...", Toast.LENGTH_LONG).show();
 6             return ;
 7         }
 8         progress.show();
 9         task = new LoginTask();
10         task.execute((Void) null);
11     }
12 });

In this way, when you click the button will pop up a schedule frame, and the new asynchronous task and implement, in the implementation process according to throw exceptions and other conditions to determine the return true/false, in the onPostExecute method the task set is empty, the schedule frame disappeared, according to the task execution results follow the corresponding treatment.


All the code:

 1 public class LoginActivity extends Activity {
 2 
 3     private Button button = null;
 4     
 5     private ProgressDialog progress = null;
 6     private LoginTask task = null;
 7     
 8     @Override
 9     protected void onCreate(Bundle savedInstanceState) {
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.activity_login);
12         
13         progress = new ProgressDialog(this);
14         progress.setMessage("Please wait ...");
15 
16         button = (Button) findViewById(R.id.button);
17         button.setOnClickListener(new OnClickListener() {
18             @Override
19             public void onClick(View v) {
20                 if(task != null) {
21                     Toast.makeText(LoginActivity.this, "Task is running ...", Toast.LENGTH_LONG).show();
22                     return ;
23                 }
24                 progress.show();
25                 task = new LoginTask();
26                 task.execute((Void) null);
27             }
28         });
29     }
30     
31     class LoginTask extends AsyncTask<Void, Void, Boolean> {
32         
33         @Override
34         protected Boolean doInBackground(Void... args) {
35             // To perform background tasks, login, query etc. . 
36             return true;
37         }
38 
39         @Override
40         public void onPostExecute(Boolean result) {
41             task = null;
42             progress.dismiss();
43             
44             if (result) {
45                 Toast.makeText(LoginActivity.this, "Success .", Toast.LENGTH_LONG).show();
46             } else {
47                 Toast.makeText(LoginActivity.this, "Failed !", Toast.LENGTH_LONG).show();
48             }
49         }
50     }
51 }

LoginActivity.java


Posted by Wythe at November 19, 2013 - 10:30 PM