android Handler和Thread实现定时器开始计时和停止计时
2014-03-31 17:43
477 查看
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>
相关文章推荐
- Android thread+handler实现获取验证码定时器
- Android Handler和ProgressBar实现进度条的开始,暂停,停止,后退和循环
- Android异步机制一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android使用Thread+Handler实现非UI线程更新界面 .
- handler的使用---如何实现Android计时与倒计时的几种方法
- Android 定时器的实现方式(TimerTask、Handler)及 Handler使用解析
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理系列文章四篇之一使用Thread+Handler实现非UI线程更新UI界面
- Android使用handler实现简单的定时器,主线程每秒刷新UI
- android之HandlerThread类的实现
- android中Handler简介&利用Handler实现计时功能
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android:定时器Timer的停止和重启实现
- Android之利用Thread.setDefaultUncaughtExceptionHandler实现崩溃监测
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面 .
- Android异步机制一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Handler和ProgressBar实现进度条的开始,暂停,停止,后退和循环
- android 待机 handler timer thread 都会停止运行