Android processing time-consuming tasks _anr

Source: Internet
Author: User


ANR

Some apps will pop up a dialog box in use, usually called the application unresponsive dialog (anr:application not responding). While this box has two options for waiting and shutting down the application, its pop-up has affected the user experience of using the app, so in general, the emergence of the ANR will be tightly controlled during Android development.

ANR Surface Reason: The application does not respond to input operations (such as input, buttons, gesture touches, etc.) for a certain amount of time (typically 5 seconds).

ANR root cause: time-consuming operation in the main thread of the Android, causing the main thread to block; The time-consuming task was not completed before the end of the Broadcastreceiver life cycle.
Android apps are running in the main thread, including activity, service life cycle, UI refresh (so the main thread is called the UI thread), input response operations, and so on. In other words, any function running in the main thread cannot take a time-consuming action, otherwise it will block the main thread and cause the input to fail to respond, thus ejecting the ANR.
The Broadcastreceiver life cycle is short (typically 10 seconds), and Android considers the program unresponsive if the time-consuming task is not completed before its life cycle ends.

These time-consuming exercises mainly include the network, the database, a large number of computational operations (such as bitmap processing) and so on.

a simple solution1, in order to prevent the main thread from being blocked, it is usually necessary to start a child thread to handle the time-consuming task, when the sub-thread's task is completed, the main thread is notified by handler, and the main thread refreshes the UI. The following is a simple example:
Import Android.app.activity;import android.os.bundle;import Android.os.handler;import Android.os.Message;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.textview;public class Mainactivity extends Activity {private TextView text;private Button button;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); text = (TextView) Findviewbyid (r.id.text); button = (button) Findviewbyid (R.id.button); Button.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {System.out.println ("- Open sub-thread "); new Taskthread (). Start ();});} Handler Handler = new Handler () {public void Handlemessage (Message msg) {switch (msg.what) {case 0:{SYSTEM.OUT.PRINTLN ("-- > Back to main thread Refresh UI task "); Text.settext (" Task Complete ");} Break;default:break;};}; Class Taskthread extends Thread {public void run () {System.out.println ("--do some time-consuming tasks"), try {sleep ($);} catch (InterRuptedexception e) {e.printstacktrace ();} Handler.sendemptymessage (0);};};
After running, print the following results:
05-26 05:30:16.490:i/system.out (381):-Turn on child threads
05-26 05:30:16.510:i/system.out (381):--do some time-consuming tasks
05-26 05:30:17.011:i/system.out (381):--back to the main thread Refresh UI task

2. The Broadcastreceiver function is also called by the main thread, but the child thread should not be started to handle time-consuming tasks. Because Broadcastreceiver belongs to an empty process, his priority is lowered after the end of the Broadcastreceiver limited life cycle, and the system easily recycles it so that the child thread's host is recycled and its child threads are killed. Therefore, it is recommended to start the service in Broadcastreceiver and start the thread processing time-consuming task in the service.





Android processing time-consuming tasks _anr

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.