Managing Manifest Receivers at Run Time in Android

Using the Package Manager, you can enable and disable any of your application’s manifest Receivers at run time using the setComponentEnabledSetting method. You can use this technique to enable or disable any application component (including Activities and Services), but it is particularly useful for manifest Receivers.

To minimize the footprint of your application, it’s good practice to disable manifest Receivers that listen for common system events (such as connectivity changes) when your application doesn’t need to respond to those events. This technique also enables you to schedule an action based on a system event — such as downloading a large fi le when the device is connected to Wi-Fi — without gaining the overhead of having the application launch every time a connectivity change is broadcast.

Dynamically toggling manifest Receivers

ComponentName myReceiverName = new ComponentName(this, MyReceiver.class);
PackageManager pm = getPackageManager();
// Enable a manifest receiver
pm.setComponentEnabledSetting(myReceiverName,
 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 
 PackageManager.DONT_KILL_APP);
// Disable a manifest receiver
pm.setComponentEnabledSetting(myReceiverName,
 PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 
 PackageManager.DONT_KILL_APP);

Leave a Comment