Skip to content
Advertisement

Toast Message is not created when message received at Java Websocket server in Android

I am trying to create basic WebSocket server with this library. I want to make a toast message when a message is received. In onMessage() function below I have implemented that. But no toast message is happening.

But Received message is printed on console.

JavaScript

Advertisement

Answer

I think you have to switch to the UI thread to show the Toast.

You can try using a Handler:

JavaScript

Or calling runOnUiThread:

JavaScript

Or broadcasting the message:

JavaScript

And registering a BroadcastReceiver in your Activity:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement