Example usage for android.app PendingIntent FLAG_CANCEL_CURRENT

List of usage examples for android.app PendingIntent FLAG_CANCEL_CURRENT

Introduction

In this page you can find the example usage for android.app PendingIntent FLAG_CANCEL_CURRENT.

Prototype

int FLAG_CANCEL_CURRENT

To view the source code for android.app PendingIntent FLAG_CANCEL_CURRENT.

Click Source Link

Document

Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one.

Usage

From source file:Main.java

public static void canalAlarm(Context context, String action) {
    Intent intent = new Intent(action);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pi);/* w w w  .j  av  a  2  s  .c o m*/
}

From source file:Main.java

public static void cancelAlarm(Context context, String action, int id) {
    Intent intent = new Intent(action);
    PendingIntent pi = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.cancel(pi);/*ww w  .  j  a  va 2s. c  o m*/
}

From source file:Main.java

public static void cancelAlarm(Context context, int id) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent("myAlarmClock");
    PendingIntent pdi = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(pdi);/* w w  w.  ja  v  a  2 s .  c  o m*/
}

From source file:Main.java

public static void exitAndRestart(Context context, Class<?> activityCls) {
    Intent intent = new Intent(context, activityCls);
    PendingIntent restartIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(1, System.currentTimeMillis() + 1000L, restartIntent);
}

From source file:Main.java

public static void stopServiceAlarm(Context context, Class<?> service) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, service);
    PendingIntent p = PendingIntent.getService(context, 10000, i, PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(p);//from w w  w.  j a  va 2 s. c om
}

From source file:Main.java

public static void startServiceAlarm(Context context, Class<?> service, long interval) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, service);
    PendingIntent p = PendingIntent.getService(context, 10000, i, PendingIntent.FLAG_CANCEL_CURRENT);
    am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 0, interval, p);
}

From source file:Main.java

public static void setAlarmTime(Context context, long timeInMillis, Intent intent) {
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent sender = PendingIntent.getBroadcast(context, intent.getIntExtra("id", 0), intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    int interval = (int) intent.getLongExtra("intervalMillis", 0);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        am.setWindow(AlarmManager.RTC_WAKEUP, timeInMillis, interval, sender);
    }/*  w  w  w .  j a v  a2s .c  o m*/
}

From source file:Main.java

public static void setTimerReceiver(Context context, int repeat_time, Class<?> class_name) {
    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intentReceiver = new Intent(context, class_name);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentReceiver,
            PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, repeat_time / 1000);

    // InexactRepeating allows Android to optimize the energy consumption
    alarm.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), repeat_time, pendingIntent);
}

From source file:Main.java

public static void cancel(Context context) {
    AlarmManager mAlarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pintent = PendingIntent.getBroadcast(context, ALARM_REQUEST, new Intent(INTENT_FILTER_NAME),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mAlarm.cancel(pintent);/*  w ww  .j  a  va 2  s. c o m*/
}

From source file:Main.java

/**
 * Restart the app./*  w w w . ja va 2  s. c  o m*/
 * @param context
 */
public static void restartApplication(Context context) {
    Intent i = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent p = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 500, p);

    //kill the application
    System.exit(0);
}