Android,Thread+Handler 线程 消息循环
Android,Thread+Handler 线程 消息循环
andriod提供了 Handler 和 Looper 来满足线程间的通信。例如一个子线程从网络上下载了一副图片,当它下载完成后会发送消息给主线程,这个消息是通过绑定在主线程的Handler来传递的。
在 Android,这里的线程分为有消息循环的线程和没有消息循环的线程,有消息循环的线程一般都会有一个Looper,这个事android的新 概念。我们的主线程(UI线程)就是一个消息循环的线程。针对这种消息循环的机制,我们引入一个新的机制Handle,我们有消息循环,就要往消息循环里 面发送相应的消息,自定义消息一般都会有自己对应的处理,消息的发送和清除,消息的的处理,把这些都封装在Handle里面,注意Handle只是针对那 些有Looper的线程,不管是UI线程还是子线程,只要你有Looper,我就可以往你的消息队列里面添加东西,并做相应的处理。
但是这里还有一点,就是只要是关于UI相关的东西,就不能放在子线程中,因为子线程是不能操作UI的,只能进行数据、系统等其他非UI的操作。
但是这里还有一点,就是只要是关于UI相关的东西,就不能放在子线程中,因为子线程是不能操作UI的,只能进行数据、系统等其他非UI的操作。
一个Handler的创建它就会被绑定到这个线程的消息队列中,如果是在主线程创建的,那就不需要写代码来创建消息队列了,默认的消息队列会在主线程被创建。但是如果是在子线程的话,就必须在创建Handler之前先初始化线程的消息队列。如下面的代码:
/** * * @author allin.dev * http://allin.cnblogs.com * */public class MainThread extends Activity {private static final String TAG = "MainThread";private Handler mMainHandler, mChildHandler;private TextView info;private Button msgBtn;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);info = (TextView) findViewById(R.id.info);msgBtn = (Button) findViewById(R.id.msgBtn);mMainHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {Log.i(TAG, "Got an incoming message from the child thread - "+ (String) msg.obj);// 接收子线程的消息info.setText((String) msg.obj);}};new ChildThread().start();msgBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (mChildHandler != null) {//发送消息给子线程Message childMsg = mChildHandler.obtainMessage();childMsg.obj = mMainHandler.getLooper().getThread().getName() + " says Hello";mChildHandler.sendMessage(childMsg);Log.i(TAG, "Send a message to the child thread - " + (String)childMsg.obj);}}});}public void onDestroy() { super.onDestroy();Log.i(TAG, "Stop looping the child thread's message queue");mChildHandler.getLooper().quit();}class ChildThread extends Thread {private static final String CHILD_TAG = "ChildThread";public void run() {this.setName("ChildThread");//初始化消息循环队列,需要在Handler创建之前Looper.prepare();mChildHandler = new Handler() {@Overridepublic void handleMessage(Message msg) { Log.i(CHILD_TAG, "Got an incoming message from the main thread - " + (String)msg.obj);try {//在子线程中可以做一些耗时的工作sleep(100);Message toMain = mMainHandler.obtainMessage();toMain.obj = "This is " + this.getLooper().getThread().getName() +". Did you send me \"" + (String)msg.obj + "\"?";mMainHandler.sendMessage(toMain);Log.i(CHILD_TAG, "Send a message to the main thread - " + (String)toMain.obj);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}};Log.i(CHILD_TAG, "Child handler is bound to - "+ mChildHandler.getLooper().getThread().getName());//启动子线程消息循环队列Looper.loop();}}}
最后完成了操作要结束子线程时,记得调用quit()来结束消息循环队列。
mChildHandler.getLooper().quit();
Activity与Thread之间的通讯
首先在Acitivity中我们要定义 一个常量来作为判断标示private static final int GUINOTIFIER = 0x1234;然后定义一些例子 里面需要的属性public Calendar mCalendar;public int mMinutes;public int mHour;public Handler mHandler;private Thread mClockThread;接着我们通过Handler来接收Thread所传递的信息mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case TestHandler.GUINOTIFIER://TestHandler是Activity的类名 //得到Handle的通知了 这个时候你可以做相应的操作,本例是使用TextView 来显示时间 mTextView .setText(mHour + " : " + mMinutes); break; } super.handleMessage(msg); } };接下来我们自定义一个Threadclass LooperThread extends Thread { public void run() { super.run(); try { do {//每间隔一秒取一次系统 时间 long time = System.currentTimeMillis(); final Calendar mCalendar = Calendar.getInstance(); mCalendar.setTimeInMillis(time); mHour = mCalendar.get(Calendar.HOUR); mMinutes = mCalendar.get(Calendar.MINUTE); Thread.sleep(1000); //取得系统时间后发送消息给Handler Message m = new Message(); m.what = Ex04_14.GUINOTIFIER; Ex04_14.this.mHandler.sendMessage(m); } while (!LooperThread.interrupted());//当系统发出终端命令时停止循环 } catch (InterruptedException e) { e.printStackTrace(); } } }
更多相关文章
- 教你如何在 Android(安卓)使用多线程下载文件
- Android(安卓)Handler 消息传递机制
- Android(安卓)多线程编程:IntentService & HandlerThread
- Android(安卓)Service——在子线程中更新UI
- 又一年对Android消息机制(Handler&Looper)的思考
- Android(安卓)Handler使用
- android - 为响应度而设计 - 开发文档翻译
- Android自用-----AsyncTask实现异步处理任务
- Android的Handler总结