How to enable and disable a component?

前端 未结 2 1692
忘了有多久
忘了有多久 2020-11-30 09:31

My initial question was basically something along the lines of this: Clearing and setting the default home application

That question was answered to my satisfaction,

相关标签:
2条回答
  • 2020-11-30 09:44

    taking Pawan approach to more generic implementation:

    public static void setComponentState(Context context, String packageName , String componentClassName, boolean enabled)
    {
        PackageManager pm  = context.getApplicationContext().getPackageManager();
        ComponentName componentName = new ComponentName(packageName, componentClassName);
        int state = enabled ?  PackageManager.COMPONENT_ENABLED_STATE_ENABLED :  PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        pm.setComponentEnabledSetting(componentName,
                state,
                PackageManager.DONT_KILL_APP);
    
    }
    
    0 讨论(0)
  • 2020-11-30 10:04

    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

    0 讨论(0)
提交回复
热议问题