Toast消息在Android的Java Websocket服务器接收到消息时未被创建。

huangapple go评论74阅读模式
英文:

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

问题

我正在尝试使用 这个 库创建基本的WebSocket服务器。
当接收到消息时,我想要显示一个提示消息。
在下面的onMessage()函数中,我已经实现了这一点。但是没有显示提示消息。

但是接收到的消息会打印在控制台上。

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("Received: " + message);
        Toast.makeText(MainActivity.this, "Received: " + message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {

    }

    @Override
    public void onStart() {

    }
};
ws.start();
英文:

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.

        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();
}

答案1

得分: 0

我认为你需要切换到UI线程来显示Toast。

你可以尝试使用Handler

new Handler(Looper.getMainLooper()).post(new Runnable() {
    public void run() {
        Toast.makeText(MainActivity.this, "接收到消息:" + message, Toast.LENGTH_SHORT).show();
    }
});

或者调用runOnUiThread

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(MainActivity.this, "接收到消息:" + message, Toast.LENGTH_SHORT).show();
    }
});

或者广播消息:

@Override
public void onMessage(WebSocket conn, String message) {
    System.out.println("接收到消息:" + 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, "接收到消息:" + 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();
}
英文:

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

You can try using a Handler:

new Handler(Looper.getMainLooper()).post(new Runnable() {
    public void run() {
        Toast.makeText(MainActivity.this, "Received: " + message, Toast.LENGTH_SHORT).show();
    }
});

Or calling runOnUiThread:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(MainActivity.this, "Received: " + message, Toast.LENGTH_SHORT).show();
    }
});

Or broadcasting the message:

@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);
}

And registering a BroadcastReceiver in your Activity:

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();
}

huangapple
  • 本文由 发表于 2020年9月20日 05:09:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63973393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定