我有一个应用程序与蓝牙设备有问题。当我断开或重新连接蓝牙设备时,应用程序似乎重新加载,或者我会说重新加载webview。它不会使应用程序崩溃,因为我正在使用A接收器捕捉连接和断开连接。这是我正在使用的代码,也是我一直坚持的地方。祝酒词起作用了,我不明白为什么它会使整个视图焕然一新。

public class MyBTReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.bluetooth.device.action.ACL_CONNECTED"))
        {
            Toast.makeText(context, "BT connect", Toast.LENGTH_SHORT).show();
        }else if(intent.getAction().equals("android.bluetooth.device.action.ACL_DISCONNECTED"))
        {
        Toast.makeText(context, "BT disconnect", Toast.LENGTH_SHORT).show();
        }
    }
}

<receiver android:name=".MyBTReceiver">
    <intent-filter>
        <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
        <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
     </intent-filter>
</receiver>

最佳答案

如果蓝牙设备被隐藏,则它将触发“键盘”配置更改事件,该事件将重新启动该活动,类似于方向更改。将configchanges=“keyboard screensize”添加到androidmanifest.xml中。我也有键盘隐藏在那里为滑出键盘,以及方向,因为我不想重新加载我的页面时,设备旋转。

10-07 19:37
查看更多