Android 中的 Java Websocket 服务器收到消息时未创建 Toast 消息
Toast Message is not created when message received at Java Websocket server in Android
我正在尝试使用 this 库创建基本的 WebSocket 服务器。
我想在收到消息时发出吐司消息。
在下面的 onMessage() 函数中,我已经实现了它。但是没有 toast 消息发生。
但是收到的消息打印在控制台上。
WebSocketServer ws = new WebSocketServer(new InetSocketAddress(3000)) {
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
conn.send("Hello world");
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}
@Override
public void onMessage(WebSocket conn, String message) {
System.out.println("Recieved: "+message);
Toast.makeText(MainActivity.this, "Recieved: " + message, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(WebSocket conn, Exception ex) {
}
@Override
public void onStart() {
}
};
ws.start();
}
我认为您必须切换到 UI 线程才能显示 Toast。
您可以尝试使用 Handler
:
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Received: " + message, Toast.LENGTH_SHORT).show();
}
});
或致电runOnUiThread
:
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Received: " + message, Toast.LENGTH_SHORT).show();
}
});
或广播消息:
@Override
public void onMessage(WebSocket conn, String message) {
System.out.println("Recieved: "+message);
Intent intent = new Intent("msgReceived");
intent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
并在您的 Activity 中注册 BroadcastReceiver:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getExtras().getString("message");
Toast.makeText(MainActivity.this, "Received: " + message, Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("msgReceived"));
@Override
protected void onStop() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onStop();
}
我正在尝试使用 this 库创建基本的 WebSocket 服务器。 我想在收到消息时发出吐司消息。 在下面的 onMessage() 函数中,我已经实现了它。但是没有 toast 消息发生。
但是收到的消息打印在控制台上。
WebSocketServer ws = new WebSocketServer(new InetSocketAddress(3000)) {
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
conn.send("Hello world");
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}
@Override
public void onMessage(WebSocket conn, String message) {
System.out.println("Recieved: "+message);
Toast.makeText(MainActivity.this, "Recieved: " + message, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(WebSocket conn, Exception ex) {
}
@Override
public void onStart() {
}
};
ws.start();
}
我认为您必须切换到 UI 线程才能显示 Toast。
您可以尝试使用 Handler
:
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Received: " + message, Toast.LENGTH_SHORT).show();
}
});
或致电runOnUiThread
:
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Received: " + message, Toast.LENGTH_SHORT).show();
}
});
或广播消息:
@Override
public void onMessage(WebSocket conn, String message) {
System.out.println("Recieved: "+message);
Intent intent = new Intent("msgReceived");
intent.putExtra("message", message);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}
并在您的 Activity 中注册 BroadcastReceiver:
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getExtras().getString("message");
Toast.makeText(MainActivity.this, "Received: " + message, Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("msgReceived"));
@Override
protected void onStop() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onStop();
}