How to use runable on the path of Android master

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.