我在Android应用的Looper
内使用Service
。通常它没有任何问题。但在某些情况下(例如,当我尝试调试我的应用程序时),它会显示应用程序内部存在竞争。 Looper
即将开始,Thread
和Handler
正在startMessageThread()
方法末尾调用的方法Service.onCreate()
中创建。
比赛是因为还有其他方法和类使用在Looper.loop();
之前初始化的类。在某些情况下,这些方法在Looper
结束之前运行。它会产生NullPointerException
。
public class MyService extends Service {
private Thread thread;
private Handler handler;
@Override
public void onCreate() {
super.onCreate();
//some code
startMessageThread();
}
private void startMessageThread() {
thread = new Thread() {
@Override
public void run() {
Looper.prepare();
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//message handling
}
}
};
MyObject myObject = new MyObject(arg1, arg2);
myObject.init();
myObject.attr = myAttr;
Looper.loop();
}
}
}
}
如果我是对的,这个问题非常相似How to create a Looper thread, then send it a message immediately?但我不想在创建'HandlerThread'之后立即发送任何消息。只是强制主线程在onCreate
方法的Looper
方法结束时等待。
我尝试在Service.onCreate
方法的末尾添加了这个:
synchronized (thread) {
try {
if (looperRun) {
wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
looperRun
当然是boolean
。我在looperRun = true;
方法调用之前添加了Looper.loor();
。这段代码在startMessageThread()
方法的末尾。
if (looperRun) {
notify();
looperRun = false;
}
在某些情况下,我有这个例外:
java.lang.RuntimeException: Unable to create service com.myapplication.service.MyService:
java.lang.IllegalMonitorStateException: object not locked by thread before wait()
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2582)
at android.app.ActivityThread.access$1800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:364)
at com.myapplication.service.MyService.onCreate(MyService.java:162)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2572)
at android.app.ActivityThread.access$1800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
修改
protected void startMessageThread() {
MessageThread messageThread = new MessageThread("messageThread");
messageThread.start();
messageHandler = new Handler(messageThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//message handling
}
}
};
}
private class MessageThread extends HandlerThread {
public MessageThread(String name) {
super(name);
}
@Override
public void run() {
Looper.prepare();
MyObject myObject = new MyObject(arg1, arg2);
myObject.init();
myObject.attr = myAttr;
Looper.loop();
}
}
编辑2
我尝试过与此类似的解决方案:How to create a Looper thread, then send it a message immediately?我的代码如下。但是我的应用程序似乎在启动后被冻结。
private class BackgroundThread extends HandlerThread {
private Handler handler;
public BackgroundThread(String name) {
super(name);
}
@Override
public void run() {
Looper.prepare();
backgroundHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
log.finest("DEAService message:" + msg.what);
switch (msg.what) {
case MSG_INIT:
MyObject myObject = new MyObject(arg1, arg2);
myObject.init();
myObject.attr = myAttr;
break;
//message handling
}
}
};
Looper.loop();
};
public synchronized void waitUntilReady() {
handler = new Handler(getMainLooper());
}
}
我像以前一样在此方法中启动此HandlerThread
:
protected void startBackgroundThread() {
BackgroundThread backgroundThread = new BackgroundThread("backgroundThread");
backgroundThread.start();
backgroundThread.waitUntilReady();
backgroundHandler.sendEmptyMessage(MSG_INIT);
}
正如我上面提到的 - 应用似乎在开始后被冻结。没有任何事情发生。
答案 0 :(得分:2)
你永远不应该阻止主线程。从未!
我已经多次使用了这个非常方便的HandlerThread
类:
HandlerThread ht = new HandlerThread("myThread");
ht.start();
Handler handler = new Handler(ht.getLooper());
handler.post(... stuff
也许你应该试一试
修改强> 对不起,有两种方法可以处理,我的原始帖子是另一种。你正在使用的方式就是这样。
HandlerThread ht = new HandlerThread("myThread");
ht.start();
Handler handler = new Handler(ht.getLooper()){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
//message handling
}
}
};
新修改:
查看代码。
protected void startMessageThread() {
HandlerThread messageThread = new HandlerThread("messageThread");
messageThread.start();
messageHandler = new Handler(messageThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case INIT:
MyObject myObject = new MyObject(arg1, arg2);
myObject.init();
myObject.attr = myAttr;
break;
}
}
};
messageHandler.sendEmptyMessage(INIT);
}
答案 1 :(得分:1)
我尝试过这段代码,一切似乎都运转良好。
private class BackgroundThread extends HandlerThread {
private Handler handler;
public BackgroundThread(String name) {
super(name);
}
@Override
public void run() {
Looper.prepare();
backgroundHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
log.finest("DEAService message:" + msg.what);
switch (msg.what) {
//message handling
}
}
};
MyObject myObject = new MyObject(arg1, arg2);
myObject.init();
myObject.attr = myAttr;
Looper.loop();
};
public synchronized void waitUntilReady() {
handler = new Handler(getMainLooper());
}
}
使用以下代码启动此HandlerThread
:
protected void startBackgroundThread() {
BackgroundThread backgroundThread = new BackgroundThread("backgroundThread");
backgroundThread.start();
backgroundThread.waitUntilReady();
}