Android Handler Demo

來源:互聯網
上載者: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;
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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.