Here’s the quick code snippet I’ve added to my growing common Android utils library:

/** 
 * Enable or disable an app component (Activity, BroadcastReceiver,
 * ContentProvider, Service).
 */
public static void enableComponent(Context context, Class<?> componentClass,
        boolean isEnable) {
    int enableFlag = isEnable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED :
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    context.getPackageManager().setComponentEnabledSetting(
            new ComponentName(context, componentClass),
            enableFlag, PackageManager.DONT_KILL_APP);
}

Example usage:

// Disable a `Service`:
enableComponent(mContext, MyServiceName.class, false);

// Enable a `BroadcastReceiver`:
enableComponent(mContext, MyReciverName.class, true);

Question: Why should app components be enabled and disabled?

Answer: Battery life and security and app features.

In the case of BroadcastReceivers, they are always listening for certain Intents and will wake up your app if that Intent occurs. You don’t want to waste users’ battery life doing nothing. For example, in LTE Discovery, there are settings options to “Start on boot” and “Disable during Wi-Fi”. If users don’t have either of those options enabled, there there is no reason for LTE Discovery to receive broadcasts when those events have occurred.

Regarding security and app features, by default all or most of your app components should already be android:exported="false", which means that other apps won’t be able to invoke or start or access them. But, there are times, when you want the Android system or other apps to be able to open an Activity, start your Service, invoke your app’s BroadcastReceiver, or access data in your ContentProvider. In those cases, only grant access when your user wants the feature.

Now a quick word from a tech company who helps support me and this site: