Look at the code:
Package Com.example.threaddemo;import Android.os.bundle;import Android.os.handler;import android.app.Activity; Import Android.util.log;import Android.view.menu;public class Threaddemo extends Activity {private static final String TA G = "Threaddemo";p rivate int count = 0; Private Handler Mhandler = new Handler (), private Runnable mrunnable = new Runnable () {@Overridepublic void run () {Lo G.E (TAG, Thread.CurrentThread (). GetName () + "" +count); ++count; Settitle ("+count"); Performed once every, seconds mhandler.postdelayed (mrunnable, n); }}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_thread_demo); Mhandler.post (mrunnable); }}
Then we press the back key to exit.
Come in again. Problem found:
That is, in addition to the previous run of the runable, there is one more
How to resolve, add in the Ondrestroy method
Mhandler.removecallbacks (mrunnable);
@Overrideprotected void OnDestroy () {mhandler.removecallbacks (mrunnable); Super.ondestroy ();}
How to use runable on the path of Android master