Before the project to do an asynchronous update UI functionality, but the results of the ANR, so want to write a demo to test exactly where the problem, the results found that the original idea is no problem, depressed ~ ~
Now this demo is a simulation of the steps inside the project
1, received the broadcast of the system (now the simulation is manually sent)
2. After the broadcast is received, handler notifies the asynchronous thread to download data from the Web, which is asynchronous (simulated as sleep)
3, after the data download handler again notify UI update
The following is the main two code that works correctly
Package Com.example.testanr;import Android.support.v7.app.actionbaractivity;import Android.content.intent;import Android.content.intentfilter;import Android.os.bundle;import Android.os.handler;import Android.os.Message;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.textview;public class Mainactivity extends Actionbaractivity { Public TextView Hellworld = null;public Button sendbroadcast = null;public final static String my_action = "Com.example.te Stanr. My_action ";p ublic static int i = 0;public Handler updateUI =new Handler () {@Overridepublic void Handlemessage (Message msg) {//TODO auto-generated Method Stubsuper.handlemessage (msg), if (Msg.arg1 ==0) {hellworld.settext ("Update ui-" + i); i++;}}; Public Handler Mhandler =new Handler () {@Overridepublic void Handlemessage (Message msg) {//TODO auto-generated method stub Super.handlemessage (msg); if (msg.arg1 = = 1) {New Thread (nEW Runnable () {@Overridepublic void Run () {//TODO auto-generated method StubSystem.out.println ("Thread ID is" +thread.cur Rentthread (). GetId () + ", and Thread name is" +thread.currentthread (). GetName ()); try {thread.currentthread (). Sleep ( 15000);} catch (Interruptedexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Message msg =new message (); Msg.arg1 =0;updateui.sendmessage (msg);}}). Start ();}}; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Hellworld = (TextView) Findviewbyid (R.id.hello_world); Sendbroadcast = (Button) Findviewbyid (r.id.sendbroadcast); Generates a Broadcastreceiver object testreceiver testreceiver = new Testreceiver (mhandler);//generates a Intentfilter object Intentfilter Filter = new Intentfilter (); Filter.addaction (mainactivity.my_action);//Register the Broadcastreceiver object with the system MainActivity.this.registerReceiver ( Testreceiver, filter); System.out. println ("Thread ID is" +thread.currentthread (). GetId () + ", and Thread name is" +thread.currentthread (). GetName ()); Sendbroadcast.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method stub//Send Broadcast Intent intent = new intent (); Intent.setaction (mainactivity.my_action); Sendbroadcast (Intent); } }); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio n Bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent Activity in Androidmanifest.xml. int id = item.getitemid (); if (id = = r.id.action_settings) {return true; } return super.onoptionsitemselected (item); }}
and a reciever.
Package Com.example.testanr;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import Android.os.handler;import Android.os.message;public class TestReceiver extends broadcastreceiver {public Handler handler;public Message message = Null;public Testreceiver (Handler Handler) { This.handler = handler;} @Overridepublic void OnReceive (context context, Intent Intent) {//TODO auto-generated method stub//Here each time new, No will error message = new Message (); message.arg1 = 1;handler.sendmessage (Message);}}
Now only to find a message can not be sent to MessageQueue inside many times, or will report such a mistake
Java.lang.IllegalStateException:The specified message Queue synchronization barrier token have not been posted or have ALR Eady been removed.
Just say this message synchronization barrier token has been sent .
But the problems in the project have not been solved, look back to find out the reasons and then sent up
We know that there are three types of ANR :
1:keydispatchtimeout (5 seconds)-- main type
Key or touch events do not respond at a specific time
2 : broadcasttimeout (seconds)
Broadcastreceiver cannot process completion at a specific time
3:servicetimeout (seconds)-- small probability type
Service cannot process completion at a specific time
So the reason or should be the second kind, probably no simulation on
Analysis of the ANR
How to investigate and resolve ANR
1: Analyze log First
2: View the call stack from the trace.txt file .
3: look at the code
4: Look closely at the cause of the ANR (iowait?block?memoryleak? )
But the project log does not output, is the strangest ~ ~ ~
Android updates the UI asynchronously by broadcasting