Android之handler使用
2010-05-28 14:52:00 来源:WEB开发网Message msg = mHandler.obtainMessage();
msg.sendToTarget();
}
class MHandler extends Handler {
public MHandler() {
}
public MHandler(Looper l) {
super(l);
}
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "first");
while (true) {
if (false)
break;
}
Log.d(TAG, "second");
}
};
class MyThread extends Thread {
@Override
public void run() {
Log.d(TAG, "first");
while (true) {
if (false)
break;
}
Log.d(TAG, "second");
}
};
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
sendMessageWithLooper();
//sendMessage();
}
public void sendMessage() {
MHandler mHandler = new MHandler();
Message msg = mHandler.obtainMessage();
msg.sendToTarget();
}
public void sendMessageWithLooper() {
HandlerThread ht = new HandlerThread("Rintail");
ht.start();
MHandler mHandler = new MHandler(ht.getLooper());
Message msg = mHandler.obtainMessage();
msg.sendToTarget();
}
class MHandler extends Handler {
public MHandler() {
}
public MHandler(Looper l) {
super(l);
}
@Override
public void handleMessage(Message msg) {
Log.d(TAG, "first");
while (true) {
if (false)
break;
}
Log.d(TAG, "second");
}
};
class MyThread extends Thread {
@Override
public void run() {
Log.d(TAG, "first");
while (true) {
if (false)
break;
}
Log.d(TAG, "second");
}
};
三。 资源回收
向Handler对象发送类似new Message ()形式的空Message可以达到清空Message的目的,这种做法与getLooper().quit()的做法是一样的。如果利用的资源较多,应及时清理。
更多精彩
赞助商链接