By using package manager you can enable or disable component declared in manifest file
There are two flag PackageManager.COMPONENT_ENABLED_STATE_DISABLED
for disable component and PackageManager.COMPONENT_ENABLED_STATE_ENABLED
for enable component.
PackageManager pm = getApplicationContext().getPackageManager();
ComponentName componentName = new ComponentName("com.app",
".broadcast_receivers.OnNetworkChangedReceiver");
pm.setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
Android manifest:
<receiver
android:name=".broadcast_receivers.OnNetworkChangedReceiver"
android:enabled="true"
>
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
Official documentation: setComponentEnabledSetting
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…