android Handler和Thread实现定时器开始计时和停止计时

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yinkgh/article/details/22683123
package com.example.timer;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView timer;//显示时间的控件
	private int recLen = 60;//定时长度
	private Button startRec, stopRec;//控制开始计时和停止计时的按钮
	private static final int START = 0;//开始计时消息标志,下面用到
	private static final int STOP = 1;//停止计时消息标志,下面用到
	private static int RUN = 0;//子线程中的while循环判断标志
	private Thread thread;

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

		timer = (TextView) findViewById(R.id.timer);

		startRec = (Button) findViewById(R.id.start);
		stopRec = (Button) findViewById(R.id.stop);
		startRec.setOnClickListener(listener);
		stopRec.setOnClickListener(listener);

	}

	public OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			int id = v.getId();
			switch (id) {
			case R.id.start:
				RUN = 0;
				thread = new Thread(new MyThread()); // start thread
				thread.start();
				break;
			case R.id.stop:
//				long tid = thread.getId();
				RUN = 1111;
				thread.interrupt();
				thread.run();
				Message message = new Message();
				message.what = STOP;
				handler.sendMessage(message);
				break;
			default:
				break;
			}

		}

	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	final Handler handler = new Handler() { // handle
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case START:
				recLen--;
				timer.setText("" + recLen);
				break;
			case STOP:
				timer.setText("" + recLen);
				break;
			}
			super.handleMessage(msg);
		}
	};

	public class MyThread implements Runnable { // thread
		@Override
		public void run() {
			while (0 == RUN) {
				try {
					Thread.sleep(100); // sleep 100ms
					Message message = new Message();
					message.what = START;
					handler.sendMessage(message);
				} catch (Exception e) {
				} 
			}
		}
	}

}

以下是界面代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button 
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始计时"
        />
    
    <Button 
        android:id="@+id/stop"
        android:layout_toRightOf="@id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止计时"
        />
    
    <TextView
        android:id="@+id/timer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="25dp"
        android:textColor="#FF0000"
        android:text="@string/hello_world" />

</RelativeLayout>


猜你喜欢

转载自blog.csdn.net/yinkgh/article/details/22683123
今日推荐