android 程序屏幕旋转,蓝牙设备断开、连接导致的Activity被重新创建的问题

主要是因为系统认为输入设备(如键盘、屏幕、导航等)发生了变化,导致重新创建界面,
修改android程序中的AndroidManifext.xml中的android:configChange

Activity下配置:android:configChanges="navigation|keyboard|keyboardHidden"
 或者:
android:configChanges="orientation|screenSize|navigation|keyboard|keyboardHidden"


orientation:屏幕方向改变
screenSize:屏幕大小改变
keyboard|keyboardHidden :键盘发生改变
navigation :导航发生改变
 
下面是官方的说明文档
https://developer.android.com/guide/topics/manifest/activity-element#config
 
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxx">
 
     ...

    <application
        android:allowBackup="true"
        android:icon="@drawable/article"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
 
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize|navigation|keyboard|keyboardHidden"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            ....
        </activity>
 
 
    </application>
 
</manifest>

参考

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,可以通过注册广播接收器来监听蓝牙设备连接断开事件。具体步骤如下: 1. 在 AndroidManifest.xml 文件中添加以下权限: ```xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> ``` 2. 创建一个广播接收器类,继承 BroadcastReceiver 并实现 onReceive 方法: ```java public class BluetoothReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) { // 蓝牙设备连接成功 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i("BluetoothReceiver", "Device connected: " + device.getName()); } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) { // 蓝牙设备连接断开 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); Log.i("BluetoothReceiver", "Device disconnected: " + device.getName()); } } } ``` 3. 在需要监听蓝牙设备连接断开事件的地方,注册广播接收器: ```java BluetoothReceiver receiver = new BluetoothReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); registerReceiver(receiver, filter); ``` 注意:在不需要监听蓝牙设备连接断开事件时,需要调用 unregisterReceiver 方法来取消注册广播接收器,以避免内存泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值