Precautions for Android asynctask

Source: Internet
Author: User

This series of articles "index of Android asynchronous processing series" on csdn gives a clear explanation of asynctask, especially some of the examples. Recently, I am working on an Android Application Development. asynctask is used to complete background tasks. Here is a brief summary.

 

Generally, asynctask is implemented by inheriting this superclass, for example:

class BackgroundTask extends AsyncTask<Object,Object,Object> 
{
@Override
protected Object doInBackground(Object... params)
{
return null;
}
}

The subclass must overload the doinbackground method. The three types in "<>" represent the execution parameter type, progress parameter type, and result parameter type in turn. The doinbackground parameter type must be an execution parameter type, and the returned type must be the same as the result parameter type. These three types should be determined as needed. In fact, objects can also be used for type conversion. Start an asynctask, which can be done in this way:

BackgroudTask bt = new BackgroundTask();
bt.execute("param");

 

An easy mistake to make when using asynctask is to directly operate the UI element in the doinbackground method. To interact with the UI, you can use publishprogress and onprogressupdate together. For example

@ Override
Protected object doinbackground (object... Params)
{
...
Publishprogress ("20% completed ");
...
Publishprogress ("80% completed ");
...
Return NULL;
}

Protected void onprogressupdate (object... progress ){
...
Textview1.settext (string) progress [0]);
...
}

Here onprogressupdate works in the UI thread.

 

Another problem with asynctask is about cancel. In fact, simply calling the cancel method of the asynctask object does not stop the execution of the doinbackground method. Generally, the acceptable method is to set a flag, that is, check the value of a variable before each execution (or you can call the iscancelled method to determine whether to continue or stop the execution. This processing method is useful for some cyclic work, but it may not be effective for some cyclic work. This is also a weakness of asynctask. Compared with thread, asynctask also has a weakness in efficiency, which can be found in the link at the beginning of this article.

 

Asynctask has another problem related to the onpreexecute method. This method works in the UI thread. Although it is called onpreexecute, but the doinbackground method (that is, the actual execute) does not wait until the onpreexecute method completes all the operations to start execution. Therefore, do not use this method. You can complete this operation before calling the execute method of the asynctask object to avoid some errors.

 

Another method of asynctask is onpostexecute, which also works in the UI thread. It is called after the doinbackground method is executed and the result is returned. In this method, you can call startactivity of the UI thread to automatically jump to the activity after a large number of background operations are completed. In this method, you can also execute another asynctask method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.