我有一个应用程序,它作为一个绑定的服务。我将消息发送到服务,但有时我收到以下错误:
I have an app which as a binded service. I am sending messages to the service, but sometimes I am getting the following error:
E / AndroidRuntime(28216):java.lang.IllegalStateException:指定的消息队列同步屏障令牌尚未发布或已经被删除
E/AndroidRuntime(28216): java.lang.IllegalStateException: The specified message queue synchronization barrier token has not been posted or has already been removed.
有时候,我得到这个错误,而不是: android.util.AndroidRuntimeException:{=什么时888 = 0}此消息已在使用中
Sometimes I get this error instead: android.util.AndroidRuntimeException: { what=888 when=0 } This message is already in use.
有时UI只是冻结。我从服务,通过处理器进行通信的活动,反之亦然。
Sometimes the UI just freezes. I am communicating from the service to the activity and visa versa through handlers.
public void init(IBinder service){
playerService = new Messenger(service);
setBound(true);
try {
Message msg = Message.obtain(null, PlayerService.MSG_REGISTER_CLIENT);
msg.replyTo = messenger;
playerService.send(msg);
while(!messageQueue.isEmpty()){
playerService.send(messageQueue.remove());
}
} catch (RemoteException e) {
// In this case the service has crashed before we could even do anything with it
Log.d(Player.TAG, "problem binding player messenger " + e.getMessage());
}
}
下面是consistenly结果上结冰,因此被称为第二次的方法。
Here is a method which consistenly results on freezes, the second time it is called.
public void play(String url) {
Message msg = Message.obtain(null, PlayerService.PLAY, 0, 0);
msg.setData(getURLBundle(url));
sendMessage(msg);
}
private void sendMessage(Message message){
if(!isBound){
Log.d(Player.TAG, "isnt bound, queueing message");
messageQueue.add(message);
}else {
try {
playerService.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
我是新的线程/使者和处理程序,所以任何帮助AP preciated,感谢:)
I'm new to Threading/ Messengers and Handlers, so any help is appreciated, thanks :)
问题是,你队列消息,由message.obtain()分配。你需要通过调用新的Message()的copyfrom(消息)来创建邮件的副本,然后在队列中添加复印的消息。
Problem is that you queue messages, that are allocated by message.obtain(). You need to create copy of message by calling new Message(), copyFrom(message) and then add copied message in queue.