Android 三種實(shí)現(xiàn)定時(shí)器詳解及實(shí)現(xiàn)方法
方法一:Handler+Thread
package com.xunfang.handerDemo; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; /** * handler定時(shí)器 * * @author Smalt * */ public class HanderDemoActivity extends Activity { TextView tvShow; private int i = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvShow = (TextView) findViewById(R.id.tv_show); new Thread(new ThreadShow()).start(); } // handler類接收數(shù)據(jù) Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 1) { tvShow.setText(Integer.toString(i++)); System.out.println("receive...."); } }; }; // 線程類 class ThreadShow implements Runnable { @Override public void run() { // TODO Auto-generated method stub while (true) { try { Thread.sleep(1000); Message msg = new Message(); msg.what = 1; handler.sendMessage(msg); System.out.println("send..."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("thread error..."); } } } } }
方法二:Handler類自帶的postDelyed
package com.xunfang.handerDemo; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.widget.TextView; /** * handler定時(shí)器使用postDelyed實(shí)現(xiàn) * * @author Smalt * */ public class HanderDemoActivity extends Activity { TextView tvShow; private int i = 0; private int TIME = 1000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvShow = (TextView) findViewById(R.id.tv_show); handler.postDelayed(runnable, TIME); //每隔1s執(zhí)行 } Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { // handler自帶方法實(shí)現(xiàn)定時(shí)器 try { handler.postDelayed(this, TIME); tvShow.setText(Integer.toString(i++)); System.out.println("do..."); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("exception..."); } } }; }
方法三:Handler+Timer+TimerTask
package com.xunfang.handerDemo; import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; /** * 定時(shí)器實(shí)現(xiàn):Handler+Timer+TimerTask * * @author Smalt * */ public class HanderDemoActivity extends Activity { TextView tvShow; private int i = 0; private int TIME = 1000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tvShow = (TextView) findViewById(R.id.tv_show); timer.schedule(task, 1000, 1000); // 1s后執(zhí)行task,經(jīng)過1s再次執(zhí)行 } Handler handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 1) { tvShow.setText(Integer.toString(i++)); } super.handleMessage(msg); }; }; Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { // 需要做的事:發(fā)送消息 Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; }
以上就是對(duì)Android 定時(shí)器的資料整理后續(xù)繼續(xù)補(bǔ)充相關(guān)知識(shí),謝謝大家對(duì)本站的支持!
- Android定時(shí)器Timer的停止和重啟實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)定時(shí)器的五種方法實(shí)例詳解
- Android 實(shí)現(xiàn)定時(shí)器的四種方式總結(jié)及實(shí)現(xiàn)實(shí)例
- Android控件Chronometer定時(shí)器的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)定時(shí)器的3種方法
- Android中使用定時(shí)器的三種方法
- Android三種實(shí)現(xiàn)定時(shí)器的方法
- 基于Android中實(shí)現(xiàn)定時(shí)器的3種解決方法
- 詳解Android實(shí)現(xiàn)定時(shí)器的幾種方法
相關(guān)文章
Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法
本篇文章小編為大家介紹,Android中 自定義數(shù)據(jù)綁定適配器BaseAdapter的方法。需要的朋友參考下2013-04-04Android自定義view 你所需要知道的基本函數(shù)總結(jié)
這篇文章主要介紹了Android自定義view 你所需要知道的基本函數(shù)的相關(guān)資料,需要的朋友可以參考下2017-02-02Android自定義狀態(tài)欄顏色與APP風(fēng)格保持一致的實(shí)現(xiàn)方法
我們知道iOS上的應(yīng)用,狀態(tài)欄的顏色總能與應(yīng)用標(biāo)題欄顏色保持一致,用戶體驗(yàn)很不錯(cuò),那安卓是否可以呢?下面小編給大家?guī)砹薃ndroid自定義狀態(tài)欄顏色與APP風(fēng)格保持一致的實(shí)現(xiàn)方法,跟著小編一起學(xué)習(xí)吧2016-10-10Android編程實(shí)現(xiàn)兩個(gè)Activity之間共享數(shù)據(jù)及互相訪問的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)兩個(gè)Activity之間共享數(shù)據(jù)及互相訪問的方法,簡單分析了Android中Activity數(shù)據(jù)共享與訪問的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android開發(fā):微信授權(quán)登錄與微信分享完全解析
本篇文章主要介紹了Android微信授權(quán)登錄與微信分享,具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11Android中利用動(dòng)態(tài)加載實(shí)現(xiàn)手機(jī)淘寶的節(jié)日特效
這篇文章主要介紹了Android中利用動(dòng)態(tài)加載實(shí)現(xiàn)手機(jī)淘寶的節(jié)日特效,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12Android在ubuntu上過濾多條關(guān)鍵字日志
今天小編就為大家分享一篇關(guān)于Android在ubuntu上過濾多條關(guān)鍵字日志,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04