前言
关键词
- android.os.Handler 、 android.os.Handler.Callback
- Looper、
- Threadle、Runnable
- Message、Message queue
android.os.Handler
倒计时程序
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center"android:id="@+id/txt"/> <Buttonandroid:id="@+id/btnStartTime"android:text="开始计时"android:layout_width="80dip"android:layout_height="wrap_content" >Button><Buttonandroid:id="@+id/btnStopTime"android:text="停止计时"android:layout_width="80dip"android:layout_height="wrap_content"/><SeekBar android:id="@+id/SeekBar01" android:layout_width="match_parent" android:layout_height="wrap_content">SeekBar> LinearLayout>
这里使用TextView 来显示倒计时的时间变化,两个按钮用于控制时间的开始和停止。SeekBar主要是用于查看线程是否被阻塞(阻塞时无法拖动)。
@Overridepublicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);txt = (TextView) findViewById(R.id.txt);btnStart = (Button) findViewById(R.id.btnStartTime);btnStop = (Button) findViewById(R.id.btnStopTime);Log.d("ThreadId", "onCread:"+ String.valueOf(Thread.currentThread().getId()));myHandler =new Handler(this);btnStart.setOnClickListener(this);btnStop.setOnClickListener(this);}
发送消息@Overridepublicvoid onClick(View v) {switch (v.getId()) {case R.id.btnStartTime:startTimer();break;case R.id.btnStopTime:timer.cancel();break;}}privatesynchronizedvoid startTimer() {timer =new Timer();// TimerTask updateTimerValuesTask = new TimerTask() {// @Override// public void run() {// updateTimerValues();// }//// };//自定义的CallBack模式。Task继承自TimerTaskTask updateTimerValuesTask =new Task(this);timer.schedule(updateTimerValuesTask, 1000, 1000);}//执行耗时的倒计时任务。 privatevoid updateTimerValues() {total--;Log.d("ThreadId", "send:"+ String.valueOf(Thread.currentThread().getId()));Message msg=new Message();Bundle date =new Bundle();// 存放数据date.putInt("time", total);msg.setData(date);msg.what=0;myHandler.sendMessage(msg);//另一种写法 // Message msg=myHandler.obtainMessage(); // Bundle date = new Bundle();// 存放数据 // date.putInt("time", total); // msg.setData(date); // msg.what=0; // msg.sendToTarget(); }@Overridepublicvoid TaskRun() {updateTimerValues();}
实现Button按钮的事件处理以此进入倒计时操作。这里使用的Timer 来执行定时操作(其实我们完全可以另起一个线程)。Task类继承了TimerTask类,里面增加了一个任务处理接口来实现回调模式,应此Activity需要实现该回调的接口 ITaskCallBack(这样做是因为我比较不喜欢内部类的编写方法)。
ICallBack接口和Task类publicinterface ITaskCallBack {void TaskRun(); }publicclass Task extends TimerTask {private ITaskCallBack iTask;public Task(ITaskCallBack iTaskCallBack){super();iTask=iTaskCallBack;}publicvoid setCallBack(ITaskCallBack iTaskCallBack){iTask=iTaskCallBack;}@Overridepublicvoid run() {// TODO Auto-generated method stub iTask.TaskRun();}}
这是Java的回调函数的一般写法。
实现CallBack/*** 实现消息处理*/@Overridepublicboolean handleMessage(Message msg) {switch(msg.what){case0:Bundle date=msg.getData();txt.setText(String.valueOf(date.getInt("time")));Log.d("ThreadId", "HandlerMessage:"+ String.valueOf(Thread.currentThread().getId()));Log.d("ThreadId", "msgDate:"+ String.valueOf(date.getInt("time")));break;}returnfalse;}
运行结果
使用Threadle进行实现
Activity类publicclass ThreadHandlerrActivity extends Activity implements Callback,OnClickListener {private TextView txt;private Button btnStart, btnStop;private Handler myHandler;private TimerThread timerThread;privateint Total=30;/** Called when the activity is first created. */@Overridepublicvoid onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);txt = (TextView) findViewById(R.id.txt);btnStart = (Button) findViewById(R.id.btnStartTime);btnStop = (Button) findViewById(R.id.btnStopTime);Log.d("ThreadId", "onCread:"+ String.valueOf(Thread.currentThread().getId()));myHandler =new Handler(this);btnStart.setOnClickListener(this);btnStop.setOnClickListener(this);}/*** 实现消息处理*/@Overridepublicboolean handleMessage(Message msg) {switch(msg.what){case0:Bundle date=msg.getData();txt.setText(String.valueOf(date.getInt("time")));Log.d("ThreadId", "HandlerMessage:"+ String.valueOf(Thread.currentThread().getId()));Log.d("ThreadId", "msgDate:"+ String.valueOf(date.getInt("time")));break;}returnfalse;}@Overridepublicvoid onClick(View v) {switch (v.getId()) {case R.id.btnStartTime://自定义的线程timerThread=new TimerThread(myHandler,60);timerThread.start();break;case R.id.btnStopTime:timerThread.stop();//timerThread.destroy(); break;}}}
自定义的线程类*** 自定义的线程类,通过传入的Handler,和Total 定期执行耗时操作* @author linzijun**/ publicclass TimerThread extends Thread {publicint Total=60;public Handler handler;/*** 初始化构造函数* @param mhandler handler 用于发送消息* @param total 总周期*/public TimerThread(Handler mhandler,int total){super();handler=mhandler;Total=total;}@Overridepublicvoid run() {while(true){Total--;if(Total<0)break;try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}Message msg=new Message();Bundle date =new Bundle();// 存放数据date.putInt("time", Total);msg.setData(date);msg.what=0;Log.d("ThreadId", "Thread:"+ String.valueOf(Thread.currentThread().getId()));handler.sendMessage(msg);}super.run();}}
这里继承了Thread类,也可以直接实现 Runnable接口。
关于POST
POSTpublicclass PostHandler extends Activity implements OnClickListener, Runnable {private TextView txt;private Button btnStart, btnStop;private Handler myHandler;private Timer timer;privateint total =60;@Overrideprotectedvoid onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stub super.onCreate(savedInstanceState);setContentView(R.layout.main);txt = (TextView) findViewById(R.id.txt);btnStart = (Button) findViewById(R.id.btnStartTime);btnStop = (Button) findViewById(R.id.btnStopTime);Log.d("ThreadId", "onCread:"+ String.valueOf(Thread.currentThread().getId()));myHandler =new Handler(){@Overridepublicvoid handleMessage(Message msg) {switch(msg.what){case0:Bundle date=msg.getData();txt.setText(String.valueOf(date.getInt("time")));Log.d("ThreadId", "HandlerMessage:"+ String.valueOf(Thread.currentThread().getId()));Log.d("ThreadId", "msgDate:"+ String.valueOf(date.getInt("time")));break;}}};btnStart.setOnClickListener(this);btnStop.setOnClickListener(this);}@Overridepublicvoid onClick(View v) {switch (v.getId()) {case R.id.btnStartTime://myHandler.post(this);myHandler.postDelayed(this, 1000);break;case R.id.btnStopTime:break;}}@Overridepublicvoid run() {while(true){total--;if(total<0)break;try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}Message msg=new Message();Bundle date =new Bundle();// 存放数据date.putInt("time", total);msg.setData(date);msg.what=0;Log.d("ThreadId", "POST:"+ String.valueOf(Thread.currentThread().getId()));myHandler.sendMessage(msg);Log.d("ThreadId", "Thread:"+ String.valueOf(Thread.currentThread().getId()));}}}
使用POST的方式 是将Runnable 一起发送给处理的线程(这里为UI),如果Runnable的操作比较耗时的话那线程将进入阻塞状态。可以看到先运行 Runnable的Run方法 然后在进入 HandleMessage() 。我还尝试了另一种写法,将TimerThreadPOST过去,运行结果是一样的。
POSTpublicclass PostHandler extends Activity implements OnClickListener, Runnable {private TextView txt;private Button btnStart, btnStop;private Handler myHandler;private Timer timer;privateint total =60;@Overrideprotectedvoid onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stub super.onCreate(savedInstanceState);setContentView(R.layout.main);txt = (TextView) findViewById(R.id.txt);btnStart = (Button) findViewById(R.id.btnStartTime);btnStop = (Button) findViewById(R.id.btnStopTime);Log.d("ThreadId", "onCread:"+ String.valueOf(Thread.currentThread().getId()));myHandler =new Handler(){@Overridepublicvoid handleMessage(Message msg) {switch(msg.what){case0:Bundle date=msg.getData();txt.setText(String.valueOf(date.getInt("time")));Log.d("ThreadId", "HandlerMessage:"+ String.valueOf(Thread.currentThread().getId()));Log.d("ThreadId", "msgDate:"+ String.valueOf(date.getInt("time")));break;}}};btnStart.setOnClickListener(this);btnStop.setOnClickListener(this);}@Overridepublicvoid onClick(View v) {switch (v.getId()) {case R.id.btnStartTime://myHandler.post(this);myHandler.postDelayed(this, 1000);break;case R.id.btnStopTime:break;}}@Overridepublicvoid run() {while(true){total--;if(total<0)break;try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch block e.printStackTrace();}Message msg=new Message();Bundle date =new Bundle();// 存放数据date.putInt("time", total);msg.setData(date);msg.what=0;Log.d("ThreadId", "POST:"+ String.valueOf(Thread.currentThread().getId()));myHandler.sendMessage(msg);Log.d("ThreadId", "Thread:"+ String.valueOf(Thread.currentThread().getId()));}}}
可以说POST的各种方法主要是用于 “按计划发送消息或执行某个Runnanble(使用POST方法)”。