Я получаю «Невозможно возобновить активность» RunTimeException, и это, очевидно, связано с моим broadcastReciever (для Bluetooth).OnPause работает хорошо.Но что не так с этим простым кодом?
//Class fields:
boolean isReceiverRegistered = false; //My flag
IntentFilter intentFilter = null;
//Inside onCreate:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
if (!isReceiverRegistered) {
registerReceiver(intentReceiver, intentFilter);
isReceiverRegistered = true;
}
protected void onResume() {
super.onResume();
if (!isReceiverRegistered) {
registerReceiver(intentReceiver, intentFilter); //THE CRASH STATEMENT
isReceiverRegistered = true;
}
}
protected void onPause() {
super.onPause();
if (isReceiverRegistered) {
try {
unregisterReceiver(intentReceiver);
} catch (IllegalArgumentException e) {
}
isReceiverRegistered = false;
}
}
BroadcastReceiver intentReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
Log.v(TAG, "connected:"+device.getName());
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
Log.v(TAG, "disconnected:"+device.getName());
}
}
};