Looper Demo
先看例子一:
- package com.example.looper_1;
- import android.app.Activity;
- import android.os.Bundle;
- import android.graphics.Color;
- import android.os.Handler;
- import android.os.Looper;
- import android.os.Message;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class ac01 extends Activity implements OnClickListener {
- private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
- private final int FP = LinearLayout.LayoutParams.FILL_PARENT;
- public TextView tv;
- private EventHandler mHandler;
- private Button btn, btn2;
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- LinearLayout layout = new LinearLayout(this);
- layout.setOrientation(LinearLayout.VERTICAL);
- btn = new Button(this);
- btn.setId(101);
- btn.setBackgroundResource(R.drawable.icon);
- btn.setText("test looper");
- btn.setOnClickListener(this);
- LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(100, 50);
- param.topMargin = 10;
- layout.addView(btn, param);
- btn2 = new Button(this);
- btn2.setId(102);
- btn2.setBackgroundResource(R.drawable.icon);
- btn2.setText("exit");
- btn2.setOnClickListener(this);
- layout.addView(btn2, param);
- tv = new TextView(this);
- tv.setTextColor(Color.WHITE);
- tv.setText("");
- LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(FP, WC);
- param2.topMargin = 10;
- layout.addView(tv, param2);
- setContentView(layout);
- }
- public void onClick(View v) {
- switch (v.getId()) {
- case 101:
- // 一個線程可以誕生一個Looper物件,由它來管理此線程裡的Message Queue
- Looper looper;
- //当程序启动时,主線程已经诞生了一个Looper物件,並且有了一個MessageQueue資料結構
- // 这个得到的就是主线程的Looper
- looper = Looper.myLooper();
- Log.i("", "looper =" + looper);
- Log.i("", "MainLooper =" + Looper.getMainLooper());
- // 誕生一個EventHandler之物件來與Looper溝通
- // Activity等物件可以藉由EventHandler物件來將訊息傳給Looper
- // 然後放入MessageQueue裡
- // EventHandler物件也扮演Listener的角色,可接收Looper物件所送來的訊息
- mHandler = new EventHandler(looper);
- // 清除整個MessageQueue裡的事件,確保不會通知到別人
- mHandler.removeMessages(0);
- // 先誕生一個Message物件,並將資料存入次物件裡
- String obj = "This my message!";
- // 組裝成一個Message物件
- Message m = mHandler.obtainMessage(1, 1, 1, obj);
- // 透過mHandler物件而將訊息m傳給looper,然後放入MessageQueue裡
- //looper物件看到MessageQueue裡有訊息m,就將它廣播出去,mHandler物件接到此訊息時
- //會呼叫其handleMessage()函數來處理之,於是輸出"This my message!"於畫面上
- mHandler.sendMessage(m);
- break;
- case 102:
- finish();
- break;
- }
- }
- // ------------------------------------------------------
- class EventHandler extends Handler {
- public EventHandler(Looper looper) {
- super(looper);
- }
- @Override
- public void handleMessage(Message msg) {
- tv.setText((String) msg.obj);
- }
- }
- }
这个例子我个人是这样来理解的: 比如一个机构(主线程),他有很多事务需要处理,所以就安排了一个部门(Looper)来专门管理这类事务的处理工作;当然了,这个机构的负责人在与Looper部门之间就需要有一个沟通的渠道,这个渠道就是我们这里所说的Handler; 如果机构需要有什么事情来处理,他就通过handler这个传话员将工作告诉Looper,然后Looper由于可能有很多工作需要处理,而来不及处理这个及时传过来的任务,所以就暂时把这个待做事项放到了后续工作日程当中,也就是我们这里所说的MessageQueue; 当过了段时间,Looper前面的事情都完成了,然后来做这个事情的时候,就将这个事情安排下去(例如:贴出告示),说这个工作需要谁来做,然后这个handler传话员又接到了这个任务安排,就马上去告知具体需要完成这个人物的当事人去做这件事情(也就是我们这里所说的handleMessage())来处理相关事情; 不晓得我这样理解对不对,希望清楚的能给予指点;哈哈.....个人觉得应该就是这个原理.
http://www.cnblogs.com/foura/articles/2016782.html