Android Handler Demo

Source: Internet
Author: User

1 package Com.mobile.free.hander;
2
3 Import android.app.Activity;
4 Import Android.os.Bundle;
5 Import Android.content.Context;
6 Import Android.os.Handler;
7 Import Android.os.HandlerThread;
8 Import Android.util.Log;
9 Import Android.view.View;
Ten import Android.view.View.OnClickListener;
Import Android.widget.Button;
12
public class Handerbox extends Activity implements Onclickliste ner
14 {
The private static final String TAG = "Handlerbox";
Private static final long delayed_time = 5000;
Handlerthread mhandlerthread = new Handlerthread ("My handle Rthread");
//mhandlerthread.start ();
Handler mhandler = null;
//new Handler (Mhandlerthread.getlooper ());

Runnable mworkrunnable = new Runnable () {
@Override
The public void run () {
LOG.D (TAG, "Workrunnable thread ID:" +
Thread.CurrentThread (). GetId ());
Mhandler.postdelayed (mworkrunnable, delayed_time);
27
28}
29};
30
Button Mstart;
Button Mstop;

/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate)
37 {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
40
Mstart = (Button) Findviewbyid (R.id.start);
Mstart.setonclicklistener (this);
Mstop = (Button) Findviewbyid (r.id.stop);
Mstop.setonclicklistener (this);
45
LOG.D (TAG, "Current thread ID:" + thread.currentthread (). GetId ());
47
Mhandlerthread.start ();
Mhandler = new Handler (Mhandlerthread.getlooper ());
50}

51
@Override
$ public void OnClick (view view) {
Switch (View.getid ()) {
Case R.id.start:
Mhandler.postdelayed (Mworkrunnable,
Delayed_time);
break;
R.id.stop Case:
Mhandler.removecallbacks (mworkrunnable);
a break;
62}
63}
64}

Android Handler Demo

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.