Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android三種實現定時器的方法

Android三種實現定時器的方法

編輯:關於Android編程

方法一、使用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;
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類接收數據
    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() {
            while (true) {
                try {
                    Thread.sleep(1000);
                    Message msg = new Message();
                    msg.what = 1;
                    handler.sendMessage(msg);
                    System.out.println("send...");
                } catch (Exception e) {
                    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;
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執行
    }
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // handler自帶方法實現定時器
            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三個Android類實現定時器

復制代碼 代碼如下:
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;
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後執行task,經過1s再次執行
    }
    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() {
            // 發送消息
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    };
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved