android handler HandlerThread 用法
一、Handler
Handler在android里负责发送和处理消息。它的主要用途有:
1)按计划发送消息或执行某个Runnanble(使用POST方法);2)从其他线程中发送来的消息放入消息队列中,避免线程冲突(常见于更新UI线程)
默认情况下,Handler接受的是当前线程下的消息循环实例(使用Handler(Looper looper)、Handler(Looper looper, Handler.Callback callback) 可以指定线程),同时一个消息队列可以被当前线程中的多个对象进行分发、处理(在UI线程中,系统已经有一个Activity来处理了,你可以再起若干个 Handler来处理)。在实例化Handler的时候,Looper可以是任意线程的,只要有Handler的指针,任何线程也都可以 sendMessage。Handler对于Message的处理不是并发的。一个Looper 只有处理完一条Message才会读取下一条,所以消息的处理是阻塞形式的(handleMessage()方法里不应该有耗时操作,可以将耗时操作放在其他线程执行,操作完后发送Message(通过sendMessges方法),然后由handleMessage()更新UI)。
HandlerThread继承于Thread,所以它本质就是个Thread。与普通Thread的差别就在于,它有个Looper成员变量。这个Looper其实就是对消息队列以及队列处理逻辑的封装,简单说就是 消息队列+消息循环。
当我们需要一个工作者线程,而不是把它当作一次性消耗品,用过即废弃的话,就可以使用它。
package com.android.settings;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class BeidouDetailSettingsActivity extends Activity {
private MyHandlerThread myHandlerThread = null;
private Button mTestBtn;
private TextView mTestTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTestBtn = (Button) findViewById(R.id.main_test_btn);
mTestTV = (TextView) findViewById(R.id.main_test_tv);
mTestBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Message msg = new Message();
msg.what = 1;
myHandlerThread.sendMessage(msg);
// mHandler.sendMessage(msg);
}
});
myHandlerThread = new MyHandlerThread("background thread.");
myHandlerThread.setmMainHandler(mHandler);
}
final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 2) {
Bundle bundle = msg.getData();
String threadID = bundle.getString("id");
String threadName = bundle.getString("name");
Time time = new Time();
time.setToNow();
Log.d("tianxuhong", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
String timeStr = time.format3339(true);
StringBuilder sb = new StringBuilder("id:").append(threadID)
.append("\nname:").append(threadName).append("\n")
.append("time:").append(timeStr);
mTestTV.setText(sb.toString());
}
}
};
}
package com.android.settings;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
/**
* Created by dev on 11/25/13.
*/
public class MyHandlerThread {
private HandlerThread mHandlerThread = null;
private MyHandler mHandler = null;
private Handler mMainHandler = null;
public MyHandlerThread(String threadName) {
super();
mHandlerThread = new HandlerThread(threadName);
mHandlerThread.start();
mHandler = new MyHandler(mHandlerThread.getLooper());
}
public void setmMainHandler(Handler handler) {
this.mMainHandler = handler;
}
public void sendMessage(Message msg) {
mHandler.sendMessage(msg);
}
class MyHandler extends Handler {
MyHandler() {
}
MyHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Message toMainMsg = mMainHandler.obtainMessage();
Log.d("tianxuhong","toMainMsg="+toMainMsg);
toMainMsg.what = 2;
Bundle bundle = new Bundle();
String threadID = String.valueOf(Thread.currentThread().getId());
String threadName = Thread.currentThread().getName();
bundle.putString("id", threadID);
bundle.putString("name", threadName);
toMainMsg.setData(bundle);
mMainHandler.sendMessage(toMainMsg);
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。