WEB开发网
开发学院手机开发Android 开发 Android之handler使用 阅读

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);}@Overridepub

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()的做法是一样的。如果利用的资源较多,应及时清理。

上一页  1 2 3 

Tags:Android handler 使用

编辑录入:coldstar [复制链接] [打 印]
赞助商链接