Set the enabled setting for a package component (activity, receiver, service, provider). - Android Activity

Android examples for Activity:Activity Feature

Description

Set the enabled setting for a package component (activity, receiver, service, provider).

Demo Code


//package com.java2s;
import android.content.ComponentName;
import android.content.Context;

import android.content.pm.PackageManager;

public class Main {
    /**/*from w w  w. java2s .  c  o  m*/
     * Set the enabled setting for a package component (activity, receiver,
     * service, provider). This setting will override any enabled state which
     * may have been set by the component in its manifest.
     * 
     * <br/>
     * <br/>
     * You can refer to <a href=
     * "http://developer.android.com/training/scheduling/alarms.html#boot"
     * >Scheduling Repeating Alarms</a> for when to use.
     * 
     * @param context
     *            The context of the current process
     * 
     * @param targetComponentClass
     *            The class object of the desired component, from which the
     *            actual class name will be retrieved
     * @param enable
     *            The new enabled state for the component
     */
    public static void setComponentEnabledSetting(Context context,
            Class<?> targetComponentClass, boolean enable) {
        int targetNewState = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;

        ComponentName target = new ComponentName(context,
                targetComponentClass);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(target, targetNewState,
                PackageManager.DONT_KILL_APP);
    }
}

Related Tutorials