1、线程消息队列有两种实现方式:
第一种:
private Handler threadHandler;
<pre name="code" class="java">class HandlerThread extends Thread
{ @Override public void run() { super.run(); Looper.prepare(); Log.d("ddd", "Thread Id:" + Thread.currentThread().getId()); threadHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); } }; Looper.loop(); } }
HandlerThread t = new HandlerThread();
t.start();
if (null != threadHandler)
{
Message msg = threadHandler.obtainMessage(i);
threadHandler.sendMessage(msg);
}
private Handler threadHandler;
android.os.HandlerThread a = new android.os.HandlerThread("AA");
a.start();
threadHandler = new Handler(a.getLooper())
{
@Override
public void handleMessage(Message msg)
{
// TODO Auto-generated method stub
super.handleMessage(msg);
Log.d("ddd", "handler Thread Id:" + Thread.currentThread().getId());
}
};
if (null != threadHandler)
{
Message msg = threadHandler.obtainMessage(i);
threadHandler.sendMessage(msg);
}