问题
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: The specified message queue synchronization barrier token has not been posted or has already been removed.
Sometimes I get this error instead:
android.util.AndroidRuntimeException: { what=888 when=0 } This message is already in use.
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());
}
}
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();
}
}
}
I'm new to Threading, Messengers and Handlers, so any help is appreciated, thanks :)
回答1:
Problem is that you queue messages, that are allocated by message.obtain()
. You need to create copy of message by calling
Message m = new Message();
m.copyFrom(message);
and only then add copied message m
into the queue.
来源:https://stackoverflow.com/questions/16550203/the-specified-message-queue-synchronization-barrier-token-has-not-been-posted