Asynchronous processing of Android

Source: Internet
Author: User

Some of the more time-consuming things in Android can't be placed in the UI thread, because the ANR (application No Response) error occurs when the time is over a period of time (about 5 seconds). So some more time-consuming operations, such as networking or reading content from a larger file, can make mistakes. Of course, there are other rules that update controls in Android only within the UI thread (because Android's design is not safe to update controls directly inside a child thread).

There are two ways to implement asynchronous processing:

1.Handler( each Handler instance will be bound to create his thread )

First of all, let me understand, I feel handler is mainly to send the message of the child thread, and then in the handler inside the update.

So it may be that time-consuming operations are placed in a child thread, and then the information is passed to handler after processing.

There are many ways to send messages to the main thread from a child thread.

A small example:

Package Zf.ui;import Com.example.android_handler. R;import android.app.activity;import android.os.bundle;import Android.os.handler;import android.os.Message;import Android.widget.textview;import Android.widget.toast;public class Mainactivity extends Activity {private TextView text; Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {//Handler receive data, then update//TODO Auto-generate D method Stubif (msg.what==1) toast.maketext (mainactivity.this, "GDD", Toast.length_short). Show (); Super.handlemessage (msg);}}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( Savedinstancestate); Setcontentview (r.layout.main_activity); text = (TextView) Findviewbyid (r.id.text); new Thread ( New Runnable () {@Overridepublic void Run () {//TODO auto-generated method stub//handles more complex events in a child thread, and then passes the information (containing some information that needs to be updated)//To Ha Ndler, and then processes the data in handler. Message message = new Message (); message.what = 1;handler.sendmessage (message);}}). Start ();}}

2.AsyncTask

Asynctask<params, Progress, Result> put more time-consuming operations in Doinbackground (Params...params), and in OnPostExecute after processing (Result Result) update.

Package Zf.ui;import Com.example.android_asynctask. R;import android.app.activity;import android.os.asynctask;import Android.os.bundle;import Android.widget.TextView; Import Android.widget.toast;public class Mainactivity extends Activity {private TextView text; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (r.layout.main_activity); text = (TextView) Findviewbyid (r.id.text); new Asynctask<object, Object, Object> () {@Overrideprotected object Doinbackground (Object ... params) {//TODO auto-generated Method Stub//in Doinba Ckground handles more complex event int n = (Integer) Params[0];long result = FIB (n); return result;} @Overrideprotected void OnPostExecute (Object result) {//TODO auto-generated method stub// Update Super.onpostexecute (Result) in the UI thread, Text.settext ("" +result.tostring ());}}.  Execute (50); Call the Execute () method to run. }public static long fib (int n) {if (N==1 | | n==0) return 1;return fib (n-1) +fib (n-2);}}

There are still a lot of imperfect places to dig into.

Asynchronous processing of Android

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.