`
onewayonelife
  • 浏览: 252439 次
  • 性别: Icon_minigender_1
  • 来自: 太原
社区版块
存档分类
最新评论

Android TimerTask

 
阅读更多

TimerTaskActivity

package org.wp.activity;

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.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TimerTaskActivity extends Activity {
	/** Handler对象 **/
	private Handler iHandler;
	/** 开启Timer **/
	private Button startButton = null;
	/** 关闭Timer **/
	private Button closeButton = null;
	/** 显示内容 **/
	private TextView myTextView = null;

	/** Timer对象 **/
	private Timer timer = null;
	/** TimerTask对象 **/
	private TimerTask timerTask = null;

	/** 执行Timer标识 **/
	private static final int LOAD_PROGRESS = 0;
	/** 关闭Timer标识 **/
	private static final int CLOSE_PROGRESS = 1;
	/** TimerId **/
	private int mTimerId = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.timer);
		addMessageHandler();

		startButton = (Button) this.findViewById(R.id.startButton);
		closeButton = (Button) this.findViewById(R.id.closeButton);
		myTextView = (TextView) this.findViewById(R.id.myTextView);
		myTextView.setText("点击按钮开始更新TimerId");

		startButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startTimer();
			}
		});

		closeButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				closeTimer();
			}
		});
	}

	private void addMessageHandler() {
		iHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case LOAD_PROGRESS:
					myTextView.setText("当前TimerId为" + msg.arg1);
					break;
				case CLOSE_PROGRESS:
					myTextView.setText("当前Timer已经关闭请重新开启");
					break;
				}
				super.handleMessage(msg);
			}
		};
	}

	private void startTimer() {
		if (timerTask == null) {
			timerTask = new TimerTask() {
				@Override
				public void run() {
					Message msg = new Message();
					msg.what = LOAD_PROGRESS;
					msg.arg1 = (int) (++mTimerId);
					iHandler.sendMessage(msg);
				}
			};
			timer = new Timer();
			// schedule(TimerTask task, long delay, long period)
			// 安排指定的任务从指定的延迟后开始进行重复的固定延迟执行。
			// task - 所要安排的任务。
			// delay - 执行任务前的延迟时间,单位是毫秒。
			// period - 执行各后续任务之间的时间间隔,单位是毫秒。
			timer.schedule(timerTask, 1000, 1000);
		}
	}

	private void closeTimer() {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
		if (timerTask != null) {
			timerTask = null;
		}
		mTimerId = 0;
		iHandler.sendEmptyMessage(CLOSE_PROGRESS);
	}
}

 

timer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent"
	android:orientation="vertical">
	<Button android:layout_width="fill_parent"	
		android:layout_height="wrap_content"
		android:text="开启TimerTask"
		android:id="@+id/startButton" />
	<Button android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="关闭TimerTask"
		android:id="@+id/closeButton" />
	<TextView android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:gravity="center"
		android:id="@+id/myTextView" />		
</LinearLayout>

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics