The specified message queue synchronization barrier token has not been posted

蓝咒 提交于 2019-12-05 05:12:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!