简体   繁体   English

Android java - 试图在新线程中显示小吃吧

[英]Android java - Trying to show snackbar in new thread

Ive been trying to make my bluetooth connection thread be able to send messages to the user through snackbars, but they never appear on the screen.我一直在尝试使我的蓝牙连接线程能够通过小吃店向用户发送消息,但它们从未出现在屏幕上。

In main method:在主要方法中:

//listener for connect button
    try {
        Button btn_connect = findViewById(R.id.btn_connect);
        btn_connect.setOnClickListener(view -> {
            if(bluetoothService.isStarted()){
                snackbarMsg(findViewById(R.id.btn_connect), "Bluetooth connection already established");
            } else{
                new Thread(() -> {
                    try {
                        Log.i(TAG, "New thread started");
                        bluetoothService.run(MainActivity.this);
                        Log.i(TAG,"Bluetooth service started");
                        snackbarMsg(findViewById(R.id.btn_connect), "Bluetooth service started");
                    } catch (Exception e) {
                        Log.e(TAG, "Bluetooth service failed", e);
                    }
                }).start();
            }
        });
    } catch (Exception exception){
        Log.e(TAG, "Bluetooth service failed");
    }

in BluetoothService class:在蓝牙服务 class 中:

public void snackbarMsg (View view, String msg){
    try {
        Snackbar snackbar = Snackbar.make(view, msg, BaseTransientBottomBar.LENGTH_SHORT);

        snackbar.show();
    } catch (Exception exception){
        Log.e(TAG, "Could not show snackbar", exception);
    }
}

The view i send with the method is always of something on the main screen, so for example using "snackbarMsg(findViewById(R.id.button_send),"Failed to find bluetooth server");"我使用该方法发送的视图总是在主屏幕上,例如使用“snackbarMsg(findViewById(R.id.button_send),"无法找到蓝牙服务器");” where button_send is on the screen i want to show the snackbar. button_send 在屏幕上我想显示小吃栏的位置。

ive tried using runnables and extends thread and whatnot.我尝试使用 runnables 并扩展线程等等。 But since i already have extensions on bluetoothservice class that didnt work, and runnable proved troublesome because i need to send context when starting the run method, and that context seemingly cant be sent at an earlier state, meaning i cant send that info when i create and object fom bluetoothservice at the beginning of the program.但是由于我已经在 bluetoothservice class 上进行了扩展,但它不起作用,并且 runnable 被证明很麻烦,因为我需要在启动运行方法时发送上下文,并且该上下文似乎无法在较早的 state 发送,这意味着我在创建时无法发送该信息和 object 来自蓝牙服务在程序的开头。

Secondly: im not sure i even need a second thread, since my bluetooth connection is only sending data, not receiving, am i just doing useless work?其次:我不确定我什至需要第二个线程,因为我的蓝牙连接只是发送数据,而不是接收,我只是在做无用的工作吗?

You need to run it on the UI/Main thread.您需要在 UI/主线程上运行它。

Use runOnUiThread(action: Runnable)使用runOnUiThread(action: Runnable)

runOnUiThread(() -> snackbarMsg(view, "insert message"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM