Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically enable/disable Broadcastreceiver

Tags:

android

We have an app that scans for Bluetooth devices. The code responsible for scanning should only run when bluetooth is enabled. Also we want to disable/enable this feature at any point in time.

We choose to implement a BroadcastReceiver that registers for the BluetoothAdapter.ACTION_STATE_CHANGED broadcast.

Here some of the problems we encountered:

Programmatically enable the BroadcastReceiver:

public void registerForBroadcasts(Context context) {
    IntentFilter bluetooth = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    context.registerReceiver(this, bluetooth);
}
  1. When the app is killed, the BroadcastReceiver is also not active anymore. So if the user multi-tasks-swipes the app to death, it is not being woken up again.
  2. We have full control, when to start the BroadcastReceiver.

Declare the BroadcastReceiver in the Manifest

<receiver android:name="com.mypackage.BroadcastReceiver">
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
    </intent-filter>
</receiver>
  1. The BroadcastReceiver is active right after the app start.
  2. The BroadcastReceiver cannot be disabled.

Declare the BroadcastReceiver in the Manifest as disabled + enable it programmatically

<receiver android:name="com.mypackage.BroadcastReceiver"
          android:enabled="false" >
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
    </intent-filter>
</receiver>

Then enable the component if you need it.

public void registerForBroadcasts(Context context) {
    ComponentName component = new ComponentName(context, BroadcastReceiver.class);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(
                component,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
}

My tests have shown, the state is persisted with the system, so the BroadcastReceiver will stay enabled. It combines the advantages of both methods.

  1. BroadcastReceiver can be disabled
  2. BroadcastReceiver can be on or off by default
  3. BroadcastReceiver keeps activation even if app is disabled and phone rebooted.

Am I missing something, does this method seem legit?

like image 626
volkersfreunde Avatar asked Jun 17 '14 14:06

volkersfreunde


People also ask

What is BroadcastReceiver?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

What is Android BroadcastReceiver?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

What file is used to register the BroadcastReceiver?

You register this broadcast receiver either in the manifest file or in the code. These events are intents. So, whenever these kinds of events happen, an intent is triggered.

Does broadcast receiver run in background?

The solution to this problem is a Broadcast Receiver and it will listen in on changes you tell it to. A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.


1 Answers

The BroadcastReceiver cannot be disabled.

Sure it can. Use PackageManager and setComponentEnabledSetting(), as you did in your third scenario.

Am I missing something, does this method seem legit?

It is very legit, at least in terms of managing the BroadcastReceiver. I don't know if there are any Bluetooth-specific hiccups, though I would doubt it. This technique is used for various broadcasts, such as only listening to ACTION_BOOT_COMPLETED when you really do have something that needs to be done at boot time.

like image 79
CommonsWare Avatar answered Oct 04 '22 15:10

CommonsWare