Android實現計時與倒數計時的幾種方法
轉自:http://blog.csdn.net/ithomer/article/details/6903084
方法一
Timer與TimerTask(Java實現)
- public class timerTask extends Activity{
- private int recLen = 11;
- private TextView txtView;
- Timer timer = new Timer();
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.timertask);
- txtView = (TextView)findViewById(R.id.txttime);
- timer.schedule(task, 1000, 1000); // timeTask
- }
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- runOnUiThread(new Runnable() { // UI thread
- @Override
- public void run() {
- recLen--;
- txtView.setText(""+recLen);
- if(recLen < 0){
- timer.cancel();
- txtView.setVisibility(View.GONE);
- }
- }
- });
- }
- };
- }
TimerTask與Handler(不用Timer的改進型)
- public class timerTask extends Activity{
- private int recLen = 11;
- private TextView txtView;
- Timer timer = new Timer();
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.timertask);
- txtView = (TextView)findViewById(R.id.txttime);
- timer.schedule(task, 1000, 1000); // timeTask
- }
- final Handler handler = new Handler(){
- @Override
- public void handleMessage(Message msg){
- switch (msg.what) {
- case 1:
- txtView.setText(""+recLen);
- if(recLen < 0){
- timer.cancel();
- txtView.setVisibility(View.GONE);
- }
- }
- }
- };
- TimerTask task = new TimerTask() {
- @Override
- public void run() {
- recLen--;
- Message message = new Message();
- message.what = 1;
- handler.sendMessage(message);
- }
- };
- }
方法三
Handler與Message(不用TimerTask)
- public class timerTask extends Activity{
- private int recLen = 11;
- private TextView txtView;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.timertask);
- txtView = (TextView)findViewById(R.id.txttime);
- Message message = handler.obtainMessage(1); // Message
- handler.sendMessageDelayed(message, 1000);
- }
- final Handler handler = new Handler(){
- public void handleMessage(Message msg){ // handle message
- switch (msg.what) {
- case 1:
- recLen--;
- txtView.setText("" + recLen);
- if(recLen > 0){
- Message message = handler.obtainMessage(1);
- handler.sendMessageDelayed(message, 1000); // send message
- }else{
- txtView.setVisibility(View.GONE);
- }
- }
- super.handleMessage(msg);
- }
- };
- }
方法四
Handler與Thread(不佔用UI執行緒)
- public class timerTask extends Activity{
- private int recLen = 0;
- private TextView txtView;
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.timertask);
- txtView = (TextView)findViewById(R.id.txttime);
- new Thread(new MyThread()).start(); // start thread
- }
- final Handler handler = new Handler(){ // handle
- public void handleMessage(Message msg){
- switch (msg.what) {
- case 1:
- recLen++;
- txtView.setText("" + recLen);
- }
- super.handleMessage(msg);
- }
- };
- public class MyThread implements Runnable{ // thread
- @Override
- public void run(){
- while(true){
- try{
- Thread.sleep(1000); // sleep 1000ms
- Message message = new Message();
- message.what = 1;
- handler.sendMessage(message);
- }catch (Exception e) {
- }
- }
- }
- }
- }
方法五
Handler與Runnable(最簡單型)
- public class timerTask extends Activity{
- private int recLen = 0;
- private TextView txtView;
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.timertask);
- txtView = (TextView)findViewById(R.id.txttime);
- handler.postDelayed(runnable, 1000);
- }
- Handler handler = new Handler();
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- recLen++;
- txtView.setText("" + recLen);
- handler.postDelayed(this, 1000);
- }
- };
- }
計時與倒數計時
方法1,方法2和方法3,都是倒數計時
方法4,方法5,都是計時
計時和倒數計時,都可使用上述方法實現(程式碼稍加改動)
UI執行緒比較
方法1,方法2和方法3,都是在UI執行緒實現的計時;
方法4和方法5,是另開Runnable執行緒實現計時
實現方式比較
方法1,採用的是Java實現,即Timer和TimerTask方式;
其它四種方法,都採用了Handler訊息處理
推薦使用
如果對UI執行緒互動要求不很高,可以選擇方法2和方法3
如果考慮到UI執行緒阻塞,嚴重影響到使用者體驗,推薦使用方法4,另起執行緒單獨用於計時和其它的邏輯處理
方法5,綜合了前幾種方法的優點,是最簡的
參考推薦:
相關文章
- Android 倒數計時的五種實現方式Android
- Android 實現計數或者倒數計時 計時器 用法DemoAndroid
- Flutter倒數計時/計時器的實現Flutter
- js實現指定時間倒數計時JS
- js實現活動倒數計時JS
- js倒數計時 實現傳送驗證碼倒數計時60sJS
- jquery實現60秒倒數計時jQuery
- 用宏實現PPT倒數計時
- Android中handler倒數計時Android
- Jquery 實現頁面倒數計時的功能jQuery
- 簡單實現時間倒數計時與伺服器同步效果伺服器
- JavaScript 前端倒數計時糾偏實現JavaScript前端
- ReactiveCocoa 實現 按鈕倒數計時React
- js實現驗證碼倒數計時JS
- 直播平臺搭建,計時和倒數計時功能的分別實現
- Android View篇之啟動頁倒數計時動畫的實現AndroidView動畫
- win10 自帶倒數計時如何設定_win10開啟計時器倒數計時的方法Win10
- Js Jquery 實現的按鈕倒數計時整理JSjQuery
- jQuery實現的按鈕可用倒數計時效果jQuery
- JavaScript倒數計時JavaScript
- js——倒數計時JS
- JS倒數計時JS
- Android使用CountDownTimer實現驗證碼倒數計時Android
- 直播小程式原始碼,vue實現時間倒數計時原始碼Vue
- iOS倒數計時的探究與選擇iOS
- Flink實時計算pv、uv的幾種方法
- js程式碼實現倒數計時秒殺的效果JS
- Android閃屏頁圓形倒數計時進度條實現Android
- CountDownTimer使用——android倒數計時Android
- Android 倒數計時類CountDownTimerAndroid
- 直播系統原始碼,實現倒數計時,定時任務原始碼
- 微信開發之小程式實現倒數計時
- Ionic Angular 實現驗證碼倒數計時功能Angular
- Kookjs 倒數計時JS
- 倒數計時34天
- 搶購倒數計時自定義控制元件的實現與優化控制元件優化
- 原生js實現的天小時分鐘和秒倒數計時效果JS
- php倒數計時出現-0的情況PHP