One, time-consuming operation
1. What is ANR
A ANR exception can occur when you execute a time-consuming code in the main thread of the application.
Time-consuming code is not executed at the end of the interface will be stuck, the user to operate the interface, 10 seconds after the time-consuming code if it is not finished, there will be a ANR exception
2. How to avoid ANR
Do not execute time-consuming code in the main thread
If you have to do time-consuming things, open a new thread and execute it in a new thread
3.UI Thread
The main thread in the Android phone is responsible for refreshing the interface and handling the user's actions
The interface of the application is created by the main thread
Changes to the interface can only be performed in the main thread
4.Handler
Sometimes we need to execute some time-consuming code, open a new thread, then need to update the interface, must operate in the main thread, then you need to use handler to communicate between threads
1) sendMessage ():
A new thread sends a message to the main thread that contains data, and the main thread gets the data in the message
Create a handler subclass object in the main thread, overriding the Handlemessage () method
A new thread can use the handler reference to call the SendMessage () method, sending a Message object
As soon as the SendMessage () method is executed, the main thread automatically executes the Handlemessage () method and receives the message object
2) post ():
A new thread sends a piece of code to the main thread, which executes the main thread directly
Create a handler object in the main thread
A new thread can use the handler call post () method to send a Runnable object
The main thread will automatically execute the runable run ()
5. Sample Code
SendMessage:
PackageCom.gnnuit.anr;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.os.SystemClock;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.TextView; Public classSendmessageactivityextendsActivity {PrivateTextView TV; PrivateHandler Handler =NewHandler () { Public voidHandlemessage (Android.os.Message msg) {//the method executes after the SendMessage () method, which is the message object sent overTv.settext (Msg.obj + "");//main thread Update interface }; }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (r.id.tv); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } Public voidGo (View v) {NewThread () { Public voidrun () { for(inti = 1; I < 10; i++) {System.out.println (i+ ""); Systemclock.sleep (1000); //Message msg = new Message ();//Create Message object, this method is not high efficiencyMessage msg = Handler.obtainmessage ();//get a message from the messages poolMsg.obj = i;//Put the data in the Message objectHandler.sendmessage (msg);//sends a Message object in a new thread, and the main thread automatically executes the Handlemessage () method } }; }.start (); }}
Post
PackageCom.gnnuit.anr;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.SystemClock;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.TextView; Public classPostactivityextendsActivity {PrivateTextView TV; PrivateHandler Handler =NewHandler (); Private inti; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV=(TextView) Findviewbyid (r.id.tv); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } Public voidGo (View v) {System.out.println ("Go:" + thread.currentthread (). GetName () + ""); NewThread () { Public voidrun () {System.out.println ("For:" + thread.currentthread (). GetName () + ""); for(i = 1;; i++) {System.out.println (i+ ""); Handler.post (NewRunnable () {//send a piece of code to the main thread using handler in a new thread, and the main thread automatically executes the run () method@Override Public voidrun () {System.out.println ("Run:" + thread.currentthread (). GetName () + ""); Tv.settext (i+ ""); } }); Systemclock.sleep (1000); } }; }.start (); }}