Looper.loop(); 之后 需要做啥?Lopper myLopper = null;
new Thread(){
public void run(){
Looper.prepare();
myLopper = Lopper.myLooper();
Looper.loop();
}
}.start();class Threadhandler extends Handler {
public void handleMessage(Message msg) {
}}
Threadhandler handler = new Threadhandler(myLopper);
new Thread(){
public void run(){
Looper.prepare();
myLopper = Lopper.myLooper();
Looper.loop();
}
}.start();class Threadhandler extends Handler {
public void handleMessage(Message msg) {
}}
Threadhandler handler = new Threadhandler(myLopper);
解决方案 »
- 求助一个问题,100分,我也没多少分了。
- 为什么想用eclipse打开一个文件的源码却找不到android项
- 开发板_android进程调试
- android 使用 selector 出错
- 现在用的最多的分辨率手机是多少?有没有哪些网站可以看这些数据?
- android布局问题
- 求思路-给附近的人主动推送消息
- 更新版本时,如何选择下载到手机存储空间还是下载到SDCARD?
- Android 开机启动问题
- 我编译的android 5.0 系统没有自动亮度调节功能,请问在那个文件配置的?
- Unfortunately, app name has stopped.
- android如何获取本地文件属性信息??
除非让loop()消息循环退出(可以调用quit()函数),否则根本无法按照你的需求来实现。
/**
* Run the message queue in this thread. Be sure to call
* {@link #quit()} to end the loop.
*/
public static void loop() {
final Looper me = myLooper();
if (me == null) {
throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
}
final MessageQueue queue = me.mQueue; // Make sure the identity of this thread is that of the local process,
// and keep track of what that identity token actually is.
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity(); for (;;) {
Message msg = queue.next(); // might block
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
} // This must be in a local variable, in case a UI event sets the logger
Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
} msg.target.dispatchMessage(msg); if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
} // Make sure that during the course of dispatching the
// identity of the thread wasn't corrupted.
final long newIdent = Binder.clearCallingIdentity();
if (ident != newIdent) {
Log.wtf(TAG, "Thread identity changed from 0x"
+ Long.toHexString(ident) + " to 0x"
+ Long.toHexString(newIdent) + " while dispatching to "
+ msg.target.getClass().getName() + " "
+ msg.callback + " what=" + msg.what);
} msg.recycle();
}
}
楼主蛋疼着- -也知道loop里面是一个循环,有消息就处理,无消息就挂起,所以想请教各位有没有啥方案解决,曲线救国也好啊~~~因为用了一个第三方框架,他的逻辑在一个单独的线程里,而且逻辑是一直执行的,就是说只要程序在运行,这个线程就在运行状态,楼主想在主线程中往这个线程里发消息~~~结果发现如上问题,无法初始化handler,不知道如何弄了
@Override
public void run() {
// TODO Auto-generated method stub
if (handler == null) {
Looper.prepare();
handler = new Handler(Looper.myLooper()){//这里加上
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what){
case do_some_th:break;
}
}
};
myHandler.sendEmptyMessage(1);
Looper.loop();
Log.e("", "这是消息循环开始之后的代码");
}
}
});//主线程给子线程发消息:
在主线程里面调用handler.sendEmptyMessage(do_some_th);就可以执行了。
不行啊,我想让Log.e("", "这是消息循环开始之后的代码");这句执行,而不是do_some_th哦,另外不能quit()looper,因为退出之后这个线程就无法继续监听主线程发来的消息了
你的线程里不是实例化了一个个Handler对象handler么?在其他线程(比如主线程)中,调用handler.sendEmptyMessage()不就接收了其他线程发来的消息么?
你的线程里不是实例化了一个个Handler对象handler么?在其他线程(比如主线程)中,调用handler.sendEmptyMessage()不就接收了其他线程发来的消息么?
实例化后必须调用Looper.loop();handler才能收到其他线程发来的消息,但是Looper.loop();里面是一个循环,会导致Looper.loop();之后的代码不执行,这样我放在Looper.loop();之后的逻辑就不能执行了