Example usage for android.content.pm PackageManager DONT_KILL_APP

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

Introduction

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

Prototype

int DONT_KILL_APP

To view the source code for android.content.pm PackageManager DONT_KILL_APP.

Click Source Link

Document

Flag parameter for #setComponentEnabledSetting(android.content.ComponentName,int,int) to indicate that you don't want to kill the app containing the component.

Usage

From source file:com.example.android.powerreceiver.MainActivity.java

@Override
protected void onStop() {
    //Disable the broadcast receiver when the app is visible
    mPackageManager.setComponentEnabledSetting(mReceiverComponentName,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    super.onStop();
}

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 {/*from  ww w  . jav a 2 s.  co 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:org.ounl.lifelonglearninghub.nfcecology.scheduler.SampleAlarmReceiver.java

/**
 * Sets an alarm/*from ww  w  .j a  va  2s . c o m*/
 * @param context
 */
public void setAlarm(Context context) {

    Log.d(CLASSNAME, "setAlarm");

    alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, SampleAlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.MINUTE, iMinsss);

    Log.d(CLASSNAME, "Setting alarm at " + calendar.getTime());

    alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

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

    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

}

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

/**
 * Sets a repeating alarm that runs once a day at approximately 8:30 a.m. When the
 * alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver.
 * @param context/* w  w w.  j  a  v a 2  s.co  m*/
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
public void setAlarm(Context context) {
    alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, NotifyAlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

    // get notification time from preferences
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
    long notificationTime = sp.getLong(SettingsFragment.KEY_PREF_NOTIFICATION_TIME, 50400000);

    Calendar calendar = Calendar.getInstance();
    //TODO: if the time has passed already, set it to tomorrow.
    calendar.setTimeInMillis(notificationTime);

    // The following line is for debug only
    //alarmMgr.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);

    // Set the alarm to fire according to the device's clock, and to repeat once a day.
    alarmMgr.setInexactRepeating(AlarmManager.RTC, notificationTime, AlarmManager.INTERVAL_DAY, alarmIntent);

    // Enable {@code SampleBootReceiver} to automatically restart the alarm when the
    // device is rebooted.
    ComponentName receiver = new ComponentName(context, NotifyAlarmReceiver.class);
    PackageManager pm = context.getPackageManager();

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

From source file:me.myatminsoe.myansms.SMSdroid.java

@Override
public void onCreate() {
    try {//from   w ww.jav  a  2  s .co  m
        Class.forName("android.os.AsyncTask");
    } catch (Throwable ignore) {
    }

    super.onCreate();
    Log.i(TAG, "init SMSdroid v" + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + ")");

    // check for default app only when READ_SMS was granted
    // this may need a second launch on Android 6.0 though
    if (hasPermission(this, Manifest.permission.READ_SMS)) {
        final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
        int state = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        if (p.getBoolean(PreferencesActivity.PREFS_ACTIVATE_SENDER, true)) {
            try {
                Cursor c = getContentResolver().query(SenderActivity.URI_SENT, PROJECTION, null, null,
                        "_id LIMIT 1");
                if (c == null) {
                    Log.i(TAG, "disable .Sender: cursor=null");
                } else if (SmsManager.getDefault() == null) {
                    Log.i(TAG, "disable .Sender: SmsManager=null");
                } else {
                    state = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
                    Log.d(TAG, "enable .Sender");
                }
                if (c != null && !c.isClosed()) {
                    c.close();
                }
            } catch (IllegalArgumentException | SQLiteException e) {

            }
        } else {
            Log.i(TAG, "disable .Sender");
        }
        getPackageManager().setComponentEnabledSetting(new ComponentName(this, SenderActivity.class), state,
                PackageManager.DONT_KILL_APP);
    } else {
        Log.w(TAG, "ignore .Sender state, READ_SMS permission is missing to check default app");
    }
}

From source file:de.ub0r.android.smsdroid.SMSdroid.java

@Override
public void onCreate() {
    try {//  ww w. ja v a2  s  . c  om
        Class.forName("android.os.AsyncTask");
    } catch (Throwable ignore) {
    }

    super.onCreate();
    Log.i(TAG, "init SMSdroid v" + BuildConfig.VERSION_NAME + " (" + BuildConfig.VERSION_CODE + ")");

    // check for default app only when READ_SMS was granted
    // this may need a second launch on Android 6.0 though
    if (hasPermission(this, Manifest.permission.READ_SMS)) {
        final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(this);
        int state = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        if (p.getBoolean(PreferencesActivity.PREFS_ACTIVATE_SENDER, true)) {
            try {
                Cursor c = getContentResolver().query(SenderActivity.URI_SENT, PROJECTION, null, null,
                        "_id LIMIT 1");
                if (c == null) {
                    Log.i(TAG, "disable .Sender: cursor=null");
                } else if (SmsManager.getDefault() == null) {
                    Log.i(TAG, "disable .Sender: SmsManager=null");
                } else {
                    state = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
                    Log.d(TAG, "enable .Sender");
                }
                if (c != null && !c.isClosed()) {
                    c.close();
                }
            } catch (IllegalArgumentException | SQLiteException e) {
                Log.e(TAG, "disable .Sender: ", e.getMessage(), e);
            }
        } else {
            Log.i(TAG, "disable .Sender");
        }
        getPackageManager().setComponentEnabledSetting(new ComponentName(this, SenderActivity.class), state,
                PackageManager.DONT_KILL_APP);
    } else {
        Log.w(TAG, "ignore .Sender state, READ_SMS permission is missing to check default app");
    }
}

From source file:com.doctoror.surprise.SurpriseActivity.java

@Override
public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
    mPackageManager.setComponentEnabledSetting(mReceiverComponent,
            isChecked ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                    : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);

    if (isChecked) {
        mAnimatorSurprise.setDisplayedChild(ANIMATOR_CHILD_PROGRESS);
        SurpriseService.executeSurprise(SurpriseActivity.this, true);
    }//ww  w  . j av  a  2  s  . c o  m
}

From source file:cn.studyjams.s2.sj0119.NForget.AlarmReceiver.java

public void setAlarm(Context context, Calendar calendar, int ID) {
    mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    // Put Reminder ID in Intent Extra
    Intent intent = new Intent(context, AlarmReceiver.class);
    intent.putExtra(ReminderEditActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
    mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    // Calculate notification time
    Calendar c = Calendar.getInstance();
    long currentTime = c.getTimeInMillis();
    long diffTime = calendar.getTimeInMillis() - currentTime;

    // Start alarm using notification time
    mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime, mPendingIntent);

    // Restart alarm if device is rebooted
    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

From source file:org.ounl.lifelonglearninghub.nfcecology.scheduler.SampleAlarmReceiver.java

/**
 * Cancels the alarm./*from  w  w  w  . j  a  v  a 2 s.c om*/
 * @param context
 */
public void cancelAlarm(Context context) {

    Log.i(CLASSNAME, "Alarm removed ");

    if (alarmMgr != null) {
        alarmMgr.cancel(alarmIntent);
    }

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

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

From source file:com.appsaur.tarucassist.AutomuteAlarmReceiver.java

public void setAlarm(Context context, Calendar calendar, int ID) {
    mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    // Put Reminder ID in Intent Extra
    Intent intent = new Intent(context, AutomuteAlarmReceiver.class);
    intent.putExtra(BaseActivity.EXTRA_REMINDER_ID, Integer.toString(ID));
    mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    // Calculate notification time
    Calendar c = Calendar.getInstance();
    long currentTime = c.getTimeInMillis();
    long diffTime = calendar.getTimeInMillis() - currentTime;

    // Start alarm using notification time
    mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + diffTime, mPendingIntent);

    // Restart alarm if device is rebooted
    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();
    pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}