3

In Play Console I see users get an IllegalStateException in the sendMessage chromecast call. According to the API:

IllegalStateException: If this method is not called on the main thread.

However in my code I call it in the following way:

            Handler uiHandler = new Handler(Looper.getMainLooper());
            uiHandler.post(new Runnable(){
                @Override
                public void run() {
                    mCastSession.sendMessage(mMyChannel.getNamespace(), message);
                }
            });

What could be the problem here? I cannot reproduce it myself.

N Jacobs
  • 341
  • 2
  • 16
  • The code looks correct in regard to run it on the UI thread. Maybe the problem is related to the value of the namespace or the message itself? As the documentation says about the namespace `Namespaces must begin with the prefix " urn:x-cast:"` – grill2010 Apr 23 '20 at 11:36
  • Thanks for the suggestion, however this looks to be correct – N Jacobs Apr 25 '20 at 19:11
  • Could you maybe add the stacktrace from the Play Console? This would help to figure out what is going wrong. – grill2010 Apr 25 '20 at 20:17

1 Answers1

0

Try putting a Log.d() inside the body of run() where you make sure the flow gets there and you can rule out that the error is in the sendMessage() method processing. Then:

runOnUiThread

Runs the specified action on the UI thread. If the current thread is the UI thread, then the action is executed immediately. If the current thread is not the UI thread, the action is posted to the event queue of the UI thread.

Handler() and runOnUiThread() can perform operations on the Thread UI, but runOnUiThread() only executes a process from a thread, and in addition a reference to the activity in which it is executed must be passed.

runOnUiThread(new Runnable() {
    public void run() {
        //...    
    }
});

GL

Source

Braian Coronel
  • 22,105
  • 4
  • 57
  • 62