Example usage for android.content.pm PackageManager setComponentEnabledSetting

List of usage examples for android.content.pm PackageManager setComponentEnabledSetting

Introduction

In this page you can find the example usage for android.content.pm PackageManager setComponentEnabledSetting.

Prototype

public abstract void setComponentEnabledSetting(@NonNull ComponentName componentName,
        @EnabledState int newState, @EnabledFlags int flags);

Source Link

Document

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

Usage

From source file:com.grarak.kerneladiutor.utils.Utils.java

public static void setStartActivity(boolean material, Context context) {
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(new ComponentName(context, StartActivity.class),
            material ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                    : PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
    pm.setComponentEnabledSetting(// www.  j a v a  2  s .  co  m
            new ComponentName(BuildConfig.APPLICATION_ID,
                    BuildConfig.APPLICATION_ID + ".activities.StartActivity-Material"),
            material ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                    : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
}

From source file:org.addhen.smssync.util.Util.java

/**
 * Makes an attempt to connect to a data network.
 *//* w w  w .ja v  a  2 s .c  o m*/
public static void connectToDataNetwork(Context context) {
    // Enable the Connectivity Changed Receiver to listen for
    // connection to a network so we can send pending messages.
    PackageManager pm = context.getPackageManager();
    ComponentName connectivityReceiver = new ComponentName(context, ConnectivityChangedReceiver.class);
    pm.setComponentEnabledSetting(connectivityReceiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

From source file:org.solovyev.android.calculator.App.java

public static void enableComponent(@Nonnull Context context, @Nonnull Class<?> componentClass, boolean enable) {
    final PackageManager pm = context.getPackageManager();

    final int componentState;
    if (enable) {
        componentState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
    } else {//from w w  w.  ja  v a 2  s  .  com
        componentState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
    }

    pm.setComponentEnabledSetting(new ComponentName(context, componentClass), componentState,
            PackageManager.DONT_KILL_APP);
}

From source file:com.android.messaging.receiver.SmsReceiver.java

/**
 * Enable or disable the SmsReceiver as appropriate. Pre-KLP we use this receiver for
 * receiving incoming SMS messages. For KLP+ this receiver is not used when running as the
 * primary user and the SmsDeliverReceiver is used for receiving incoming SMS messages.
 * When running as a secondary user, this receiver is still used to trigger the incoming
 * notification.//from  w w w.j a va 2 s.co  m
 */
public static void updateSmsReceiveHandler(final Context context) {
    boolean smsReceiverEnabled;
    boolean mmsWapPushReceiverEnabled;
    boolean respondViaMessageEnabled;
    boolean broadcastAbortEnabled;

    if (OsUtil.isAtLeastKLP()) {
        // When we're running as the secondary user, we don't get the new SMS_DELIVER intent,
        // only the primary user receives that. As secondary, we need to go old-school and
        // listen for the SMS_RECEIVED intent. For the secondary user, use this SmsReceiver
        // for both sms and mms notification. For the primary user on KLP (and above), we don't
        // use the SmsReceiver.
        smsReceiverEnabled = OsUtil.isSecondaryUser();
        // On KLP use the new deliver event for mms
        mmsWapPushReceiverEnabled = false;
        // On KLP we need to always enable this handler to show in the list of sms apps
        respondViaMessageEnabled = true;
        // On KLP we don't need to abort the broadcast
        broadcastAbortEnabled = false;
    } else {
        // On JB we use the sms receiver for both sms/mms delivery
        final boolean carrierSmsEnabled = PhoneUtils.getDefault().isSmsEnabled();
        smsReceiverEnabled = carrierSmsEnabled;

        // On JB we use the mms receiver when sms/mms is enabled
        mmsWapPushReceiverEnabled = carrierSmsEnabled;
        // On JB this is dynamic to make sure we don't show in dialer if sms is disabled
        respondViaMessageEnabled = carrierSmsEnabled;
        // On JB we need to abort broadcasts if SMS is enabled
        broadcastAbortEnabled = carrierSmsEnabled;
    }

    final PackageManager packageManager = context.getPackageManager();
    final boolean logv = LogUtil.isLoggable(TAG, LogUtil.VERBOSE);
    if (smsReceiverEnabled) {
        if (logv) {
            LogUtil.v(TAG, "Enabling SMS message receiving");
        }
        packageManager.setComponentEnabledSetting(new ComponentName(context, SmsReceiver.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    } else {
        if (logv) {
            LogUtil.v(TAG, "Disabling SMS message receiving");
        }
        packageManager.setComponentEnabledSetting(new ComponentName(context, SmsReceiver.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }
    if (mmsWapPushReceiverEnabled) {
        if (logv) {
            LogUtil.v(TAG, "Enabling MMS message receiving");
        }
        packageManager.setComponentEnabledSetting(new ComponentName(context, MmsWapPushReceiver.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    } else {
        if (logv) {
            LogUtil.v(TAG, "Disabling MMS message receiving");
        }
        packageManager.setComponentEnabledSetting(new ComponentName(context, MmsWapPushReceiver.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }
    if (broadcastAbortEnabled) {
        if (logv) {
            LogUtil.v(TAG, "Enabling SMS/MMS broadcast abort");
        }
        packageManager.setComponentEnabledSetting(new ComponentName(context, AbortSmsReceiver.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        packageManager.setComponentEnabledSetting(new ComponentName(context, AbortMmsWapPushReceiver.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    } else {
        if (logv) {
            LogUtil.v(TAG, "Disabling SMS/MMS broadcast abort");
        }
        packageManager.setComponentEnabledSetting(new ComponentName(context, AbortSmsReceiver.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        packageManager.setComponentEnabledSetting(new ComponentName(context, AbortMmsWapPushReceiver.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }
    if (respondViaMessageEnabled) {
        if (logv) {
            LogUtil.v(TAG, "Enabling respond via message intent");
        }
        packageManager.setComponentEnabledSetting(
                new ComponentName(context, NoConfirmationSmsSendService.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    } else {
        if (logv) {
            LogUtil.v(TAG, "Disabling respond via message intent");
        }
        packageManager.setComponentEnabledSetting(
                new ComponentName(context, NoConfirmationSmsSendService.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    }
}

From source file:com.google.android.gm.ay.java

public static void bg(final Context context) {
    final boolean bj = bj(context);
    final PackageManager packageManager = context.getPackageManager();
    ComponentName componentName;/*from  w  ww.j a  v  a 2 s  .  c  o m*/
    if (bj) {
        componentName = ay.aZe;
    } else {
        componentName = ay.aZd;
    }
    ComponentName componentName2;
    if (bj) {
        componentName2 = ay.aZd;
    } else {
        componentName2 = ay.aZe;
    }
    packageManager.setComponentEnabledSetting(componentName2, 2, 1);
    packageManager.setComponentEnabledSetting(componentName, 1, 1);
}

From source file:com.tonikorin.cordova.plugin.autostart.AutoStart.java

private void setAutoStart(boolean enabled) {

    Context context = cordova.getActivity().getApplicationContext();
    int componentState;
    SharedPreferences sp = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    if (enabled) {
        componentState = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
        // Store the class name of your main activity for AppStarter
        editor.putString(CLASS_NAME, cordova.getActivity().getLocalClassName());
    } else {/* ww w  .j  ava2  s.c  o  m*/
        componentState = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        editor.remove(CLASS_NAME);
    }
    editor.commit();
    // Enable or Disable BootCompletedReceiver
    ComponentName bootCompletedReceiver = new ComponentName(context, BootCompletedReceiver.class);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(bootCompletedReceiver, componentState, PackageManager.DONT_KILL_APP);
}

From source file:townley.stuart.app.android.trekkinly.AlertClass.java

@Override
public void onReceive(final Context context, Intent intent) {

    NotificationItemClass notificationItemClass = new NotificationItemClass();
    DataBaseHelper db = new DataBaseHelper(context);
    db.getWritableDatabase();//from  ww  w .  j av a 2s .  co  m
    db.getNotification(notificationItemClass);

    if (db.notification <= System.currentTimeMillis()) {
        createNotification(context, "Something is missing!", "Click here for more details!", "Alert");

        ComponentName receiver = new ComponentName(context, AlertClass.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);

        intent = new Intent(context, AlertClass.class);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(PendingIntent.getBroadcast(context.getApplicationContext(), 1, intent,
                PendingIntent.FLAG_UPDATE_CURRENT));
    }

    else if (db.notification > System.currentTimeMillis()) {
        intent = new Intent(context, AlertClass.class);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, db.notification, PendingIntent
                .getBroadcast(context.getApplicationContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    }
}

From source file:net.eledge.android.europeana.service.receiver.BlogCheckerReceiver.java

public void disableBlogChecker(Context context) {
    if (alarmManager != null) {
        alarmManager.cancel(blogCheckerIntent);
    }/*from  w  ww  .jav a2 s. c o m*/

    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();

    if (pm != null) {
        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
}

From source file:net.eledge.android.europeana.service.receiver.BlogCheckerReceiver.java

public void enableBlogChecker(Context context) {
    alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, BlogCheckerReceiver.class);
    blogCheckerIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, INTERVAL, INTERVAL,
            blogCheckerIntent);/*from   w  w w .j a v  a 2s. c  o m*/

    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();

    if (pm != null) {
        pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }
}

From source file:eu.istvank.apps.lenslog.receivers.NotifyAlarmReceiver.java

/**
 * Cancels the alarm./*w  w  w.  j  a  v  a2 s  . com*/
 * @param context
 */
public void cancelAlarm(Context context) {
    // If the alarm has been set, cancel it.
    if (alarmMgr != null) {
        alarmMgr.cancel(alarmIntent);
    }

    // Disable {@code SampleBootReceiver} so that it doesn't automatically restart the
    // alarm when the device is rebooted.
    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);
}