码迷,mamicode.com
首页 > 移动开发 > 详细

Android Handler Demo

时间:2015-08-21 13:01:24      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:

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;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12
13 public class HanderBox extends Activity implements OnClickListe ner
14 {
15 private static final String TAG = "HandlerBox";
16 private static final long DELAYED_TIME = 5000;
17 HandlerThread mHandlerThread = new HandlerThread("My handle rthread");
18 //mHandlerThread.start();
19 Handler mHandler = null;
20 //new Handler(mHandlerThread.getLooper());

21 Runnable mWorkRunnable = new Runnable() {
22 @Override
23 public void run() {
24 Log.d(TAG, "WorkRunnable thread id:" +
25 Thread.currentThread().getId());
26 mHandler.postDelayed(mWorkRunnable, DELAYED_TIME);
27
28 }
29 };
30
31 Button mStart;
32 Button mStop;

34 /** Called when the activity is first created. */
35 @Override
36 public void onCreate(Bundle savedInstanceState)
37 {
38 super.onCreate(savedInstanceState);
39 setContentView(R.layout.main);
40
41 mStart = (Button)findViewById(R.id.start);
42 mStart.setOnClickListener(this);
43 mStop = (Button)findViewById(R.id.stop);
44 mStop.setOnClickListener(this);
45
46 Log.d(TAG, "Current thread id:" + Thread.currentThread( ).getId());
47
48 mHandlerThread.start();
49 mHandler = new Handler(mHandlerThread.getLooper());
50 }

51
52 @Override
53 public void onClick(View view) {
54 switch (view.getId()) {
55 case R.id.start:
56 mHandler.postDelayed(mWorkRunnable,
57 DELAYED_TIME);
58 break;
59 case R.id.stop:
60 mHandler.removeCallbacks(mWorkRunnable);
61 break;
62 }
63 }
64 }

Android Handler Demo

标签:

原文地址:http://www.cnblogs.com/qingxueyunfeng/p/4747278.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!