The exception that appears when you update the UI (Program interface) in a non-UI thread is as follows:
So how do you get a finer UI in a non-UI thread?
There are many ways to do this, and here are two main types:
The first type: Add looper.prepare () after the line of code that needs to update the UI; With Looper.loop (); Two words can be. Such as:
New Thread () {@Overridepublic void Run () {//TODO auto-generated method Stubtxtrotation.settext ("Update ui! in a non-UI thread "); Looper.prepare (); Looper.loop ();}}. Start ();
The second type: Use the following method:
New Thread () {@Overridepublic void Run () {//TODO auto-generated method Stubshowtoastbyrunnable (Mainactivity.this, "", 3000);}}. Start ();
/** * Use toast in a non-UI thread * @param Context Context * @param text to display the message content * @param the time the duration message was displayed * */private void Showtoastbyru Nnable (Final context context, final charsequence text, final int duration) { Handler Handler = new Handler (Looper.getm Ainlooper ()); Handler.post (New Runnable () { @Override public void Run () { Toast.maketext (context, text, duration). Show ( ); } });}
Android to update UI in non-UI threads