蓝牙连接状态发生变化时阻止onDestroy

时间:2017-06-05 11:26:34

标签: android android-intent broadcastreceiver android-bluetooth

目标

  • 如果已连接的蓝牙设备已断开连接且活动已在运行,请关闭活动

问题

  • 当蓝牙设备连接状态通过 BluetoothAdapterProperties:CONNECTION_STATE_CHANGE 更改时,似乎创建了一个新的活动或当前的活动重新启动。

代码中没有任何内容可以监听和/或应对蓝牙连接状态的变化作出反应。

问题表现在BroadcastReceivers的使用上,而BroadcastReceivers又使用意图启动Activity。出于某种原因,即使蓝牙连接中唯一的变化是 BluetoothAdapterProperties,活动也会在其生命周期中运行,产生新的窗口:CONNECTION_STATE_CHANGE

我已经在使用Android N的Nexus 6P上进行了测试。我还不知道这个实现对于任何其他设备意味着什么样的影响。但我至少需要在一个设备上使用它。

更新

我做了一些实验,发现如果我没有在AndroidManifest中注册BroadcastReceiver,那么调用onDestroy的问题就会消失。但是,我希望能够对蓝牙连接设备作出反应,以便我可以启动我的活动,然后处理输入。如果每次新设备连接/断开连接时活动都会被破坏,那么这根本不会起作用。如果BroadcastReceiver已经在运行并且我可以控制该行为,那么推理广播活动的原因是什么?

更新2

我还可以得出结论,使用此方法https://stackoverflow.com/a/6529365/975641禁用静态声明的BroadcastReceiver并不能改进。一旦Manifest-BroadcastReceiver从Android捕获ACL_CONNECTED意图并启动我的自定义活动,当连接状态发生变化时(通常在ACL_DISCONNECTED之前),它将无情地在其上调用onDestroy。如果我在Manifest中声明了ACL_DISCONNECTED,则无关紧要。只要我的接收器侦听ACL_CONNECTED意图并基于此启动我的Activity,就会在连接状态发生变化时调用onDestroy。太令人沮丧了。

清单

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.VIBRATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".BtActivity"
        android:launchMode="singleTop" />
    <receiver android:name=".BtConnectionBroadcastReceiver" android:priority="100000">
        <intent-filter>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
            <action android:name="android.intent.action.MEDIA_BUTTON" />
            <action android:name="android.media.VOLUME_CHANGED_ACTION" />
        </intent-filter>
    </receiver>
</application>

BtConnectionBroadcastReceiver

public class BtConnectionBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "BT";
    public static final String BROADCAST_ACTION_CONNECTED = "CONNECTED";
    public static final String BROADCAST_ACTION_DISCONNECTED = "DISCONNECTED";
    SharedPreferences mSharedPreferences;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        // When discovery finds a device
        if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
            // Get the BluetoothDevice object from the Intent
            Log.d(TAG, "DEVICE CONNECTED");
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.d("DEVICE NAME", device.getName());
            Log.d("DEVICE ADDRESS", device.getAddress());
            Intent i = new Intent(context, BtActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            context.startActivity(i);
        } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
            Log.d(TAG, "DEVICE DISCONNECTED");
            intent = new Intent();
            intent.setAction(BtConnectionBroadcastReceiver.BROADCAST_ACTION_DISCONNECTED);
            context.sendBroadcast(intent);
        }
    }

BtActivity

public class BtActivity extends AppCompatActivity {
private static final String TAG = "BT";
Window mWindow;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_bt);

    Log.d(TAG, "onCreate");
    IntentFilter filter = new IntentFilter(BtConnectionBroadcastReceiver.INTENT_FILTER);
    filter.addAction(BtConnectionBroadcastReceiver.BROADCAST_ACTION_CONNECTED);
    filter.addAction(BtConnectionBroadcastReceiver.BROADCAST_ACTION_DISCONNECTED);
    //registerReceiver(mReceiver, filter);

    mWindow = getWindow();
    WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    //params.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
    params.screenBrightness = 0.2f;
    mWindow.setAttributes(params);
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    mWindow.getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                    View.SYSTEM_UI_FLAG_FULLSCREEN |
                    View.SYSTEM_UI_FLAG_IMMERSIVE);
    }

@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, "onResume");
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy");
}

BroadcastReceiver mReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "BROADCAST RECEIVED IN ACTIVITY");
        String mac;
        if(intent.getAction().equals(BtConnectionBroadcastReceiver.BROADCAST_DEVICE_CONNECTED)) {
            Log.d(TAG, "CONNECT BROADCAST RECEIVED");
            mac = intent.getStringExtra("mac");
            checkConnectedDevice(mac, true); // This adds a device to an internal list
            Log.d(TAG, "Activity nr of devices:" +mNrOfDevices);
        }
        if(intent.getAction().equals(BtConnectionBroadcastReceiver.BROADCAST_DEVICE_DISCONNECTED)) {
            Log.d(TAG, "DISCONNECT BROADCAST RECEIVED");
            mac = intent.getStringExtra("mac");
            checkConnectedDevice(mac, false); // This removes a device from an internal list
            Log.d(TAG, "Activity nr of devices:" +mNrOfDevices);
            if(mNrOfDevices < 1) {
                Log.d(TAG, "No more connected devices");
                finish();
            }
        }
        abortBroadcast();
    }
};
}

当我运行此代码时,我得到以下链:

  1. 启动MainActivity(不包括在内,它只包含一个具有默认主布局的活动,以便注册应用程序接收器)
  2. 打开蓝牙设备(这已经配对了,所以android知道了)
  3. 等到它连接并得到这个:
    • DEVICE CONNECTED
    • 的onCreate
    • 的onResume
  4. 关闭蓝牙设备然后我得到这个:
    • DEVICE DISCONNECTED
    • 的onDestroy
    • 的onCreate
    • 的onResume
  5. 我无法理解为什么活动会在此时重新启动。活动已在运行,BroadcastReceiver仅向已经运行的活动发送广播。我无法弄清楚为什么活动会自行杀死而然后重新启动。这使我处于Activity仍然运行的状态,但它不是最初启动的Activity。

    然而,我确实在logcats中看到了与此有关的东西,并且它在此排序中;

    06-02 15:45:09.156 26431 26431 D BT:设备已断开连接

    06-02 15:45:09.213 19547 19547 D BluetoothAdapterService:handleMessage() - MESSAGE_PROFILE_CONNECTION_STATE_CHANGED

    06-02 15:45:09.213 26431 26431 D BT:onDestroy

    06-02 15:45:09.214 19547 19547 D BluetoothAdapterProperties:CONNECTION_STATE_CHANGE:FF:FF:20:00:C1:47:2 - &gt; 0

    06-02 15:45:09.216 3502 3805 D CachedBluetoothDevice:onProfileStateChanged:profile HID newProfileState 0

    06-02 15:45:09.237 414 414 W SurfaceFlinger:无法登录二进制事件日志:溢出。

    06-02 15:45:09.239 26431 26431 D BT:onCreate

    06-02 15:45:09.243 26431 26431 D BT:onResume

3 个答案:

答案 0 :(得分:0)

读过这个https://developer.android.com/guide/components/broadcasts.html#effects_on_process_state我可以安全地得出结论,调用onDestroy的原因是因为接收器影响了它运行的过程,这实际上意味着当接收器运行其onReceive方法时,它将摧毁自己并用它来进行活动。

我当然希望它的工作方式不同,但我相信这是有效的,需要采取另一种方法。

答案 1 :(得分:0)

在AndroidManifest.xml中为“活动”添加以下内容,它对我有用。

android:configChanges="keyboard|keyboardHidden"

答案 2 :(得分:0)

我知道这个答案很晚了,但我的活动标签遇到了这个问题。在清单文件中,我为 configChange 添加了以下行。

android:configChanges="keyboard|orientation|screenSize|keyboardHidden|navigation|screenLayout"

现在我的应用程序不会自行终止。