Dealing with BroadcastReceivers on demand
A side effect of using BroadcastReceivers is that each time one of the events is actually happening, the device will wake up. This means that a small amount of energy is not to be despised if we consider the long term.
We can use an auxiliary technique here to make our application more efficient: activating or deactivating BroadcastReceivers on demand, based on the current status of the cell phone. That means: if, for example, the Internet connectivity has been lost, we might only want to wait until the Internet connection is active and dismiss the other BroadcastReceivers, since they will not be useful anymore.
The following code snippet shows how to activate or deactivate components that have been defined in the PackageManager
class programmatically:
ComponentName myReceiver = new ComponentName(context, Receiver.class); PackageManager packageManager = getPackageManager(); packageManager.setComponentEnabledSetting(myReceiver, PackageManager...