, Android on the new line Cheng want to update the UI, need to jump to the main thread in order to operate, the following is the foreigner to give a few programs, we learn a lot.
1 post a runnable subclass through the UI control:
private void LoadIcon () {New Thread (new Runnable () {@Overridepublic void run () {try {thread.sleep (mdelay);} catch (Interru Ptedexception e) {e.printstacktrace ();} Mbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.painter); Mimageview.post (new Runnable () {@ overridepublic void Run () {mimageview.setimagebitmap (MBITMAP);}}). Start ();}
2. Perform the modification by calling the main thread Runonuithread
New Thread (New Runnable () {@Overridepublic void run () {try {thread.sleep (mdelay);} catch (Interruptedexception e) {E.prin Tstacktrace ();} Mbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.painter); SimpleThreadingExample.this.runOnUiThread (New Runnable () {@Overridepublic void run () {Mimageview.setimagebitmap ( MBITMAP);}). Start ();
3. Perform the modification by handler the post one runnable on the other thread continuously:
Private class Loadicontask implements Runnable {int resId; Loadicontask (int resId) {this.resid = resId;} public void Run () {Handler.post (new Runnable () {@Overridepublic void run () {mprogressbar.setvisibility ( progressbar.visible);}); Mbitmap = Bitmapfactory.decoderesource (Getresources (), resId);//simulating long-running operationfor (int i = 1; i < 1 1; i++) {sleep (); final int step = i;handler.post (new Runnable () {@Overridepublic void run () {mprogressbar.setprogress (step * 10);});} Handler.post (New Runnable () {@Overridepublic void run () {mimageview.setimagebitmap (MBITMAP);}}); Handler.post (New Runnable () {@Overridepublic void run () {mprogressbar.setvisibility (progressbar.invisible);}});}
4. The following example is done by Handlemessage, and it is interesting to use a soft reference. (Refer to the weak keyword in ios), while handler sends a message obtainmessage uses a pool recycling mechanism.
Static class Uihandler extends Handler {weakreference
5. Adopt the Aynctask way, is also our more commonly used way
Class Loadicontask extends Asynctask<integer, Integer, bitmap> {@Overrideprotected void OnPreExecute () { Mprogressbar.setvisibility (progressbar.visible);} @Overrideprotected Bitmap doinbackground (Integer ... resId) {Bitmap tmp = Bitmapfactory.decoderesource (Getresources (), RESID[0]);//simulating long-running operation for (int i = 1; i <; i++) {sleep ();p ublishprogress (i * 10);} return TMP;} @Overrideprotected void Onprogressupdate (Integer ... values) {mprogressbar.setprogress (values[0]);} @Overrideprotected void OnPostExecute (Bitmap result) {mprogressbar.setvisibility (progressbar.invisible); Mimageview.setimagebitmap (result);} private void Sleep () {try {thread.sleep (mdelay)} catch (Interruptedexception e) {log.e (TAG, e.tostring ());}}}
No matter what. Hair this thing, just want to express, the foreigner's things are sometimes quite interesting.
android-threads update the UI in several ways