Example usage for android.app PendingIntent cancel

List of usage examples for android.app PendingIntent cancel

Introduction

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

Prototype

public void cancel() 

Source Link

Document

Cancel a currently active PendingIntent.

Usage

From source file:com.glanznig.beepme.BeeperApp.java

public void updateTimer(int status) {
    if (status != 3 || status != 4) {
        Intent intent = new Intent(this, BeepActivity.class);
        PendingIntent alarmIntent = PendingIntent.getActivity(this, ALARM_INTENT_ID, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        alarmIntent.cancel();
    }//from ww  w  . j  a v a  2s. c o m
    new ScheduledBeepTable(this.getApplicationContext()).updateStatus(getPreferences().getScheduledBeepId(),
            status);
    if (status != 3) {
        getPreferences().setScheduledBeepId(0L);
    }
}

From source file:org.chromium.ChromeNotifications.java

private void clear(final CordovaArgs args, final CallbackContext callbackContext) {
    try {//from  w w  w  . j ava  2s .co m
        String notificationId = args.getString(0);
        PendingIntent pendingIntent = makePendingIntent(NOTIFICATION_CLICKED_ACTION, notificationId, -1,
                PendingIntent.FLAG_NO_CREATE);
        if (pendingIntent != null) {
            notificationManager.cancel(notificationId.hashCode());
            pendingIntent.cancel();
            callbackContext.success(1);
        } else {
            callbackContext.success(0);
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "Could not clear notification", e);
        callbackContext.error("Could not clear notification");
    }
}

From source file:se.hyperlab.tigcm.TiGCMModule.java

@Kroll.method
public void clearSchedule() {
    TiApplication app = TiApplication.getInstance();

    int ntfCount = app.getAppProperties().getInt(PROPERTY_NOTIFICATION_COUNTER, 0);

    Log.d(TAG, "Clearing " + ntfCount + " notifications");

    if (ntfCount > 0) {
        Intent intent = new Intent(app.getApplicationContext(), NotificationPublisher.class);
        for (int i = 0; i < ntfCount; i++) {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(app.getApplicationContext(), i, intent,
                    PendingIntent.FLAG_ONE_SHOT);
            AlarmManager alarmManager = (AlarmManager) app.getApplicationContext()
                    .getSystemService(Context.ALARM_SERVICE);
            alarmManager.cancel(pendingIntent);
            pendingIntent.cancel();
        }//from ww w  .j av  a2s  .c o m

        app.getAppProperties().setInt(PROPERTY_NOTIFICATION_COUNTER, 0);
    }
}

From source file:at.ac.uniklu.mobile.sportal.service.MutingService.java

/**
 * Turns OFF automatic ringtone muting during courses.
 *//* w ww  .  jav  a  2 s  .co  m*/
private void turnOff() {
    Log.d(TAG, "turnOff()");
    Analytics.onEvent(Analytics.EVENT_MUTINGSERVICE_OFF);

    // if the phone is currently in a muting period, turn the ringtone back on before turning off the service
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    if (Preferences.isMutingPeriod(preferences)) {
        MutingUtils.ringtoneTurnOn(this);
        Preferences.setMutingPeriod(preferences, false);
    }

    // remove eventually existing user notification
    removeNotification(Studentportal.NOTIFICATION_MS_INFO);

    // cancel an eventually existing pending alarm and the beloging intent as well (otherwise isRunning would always return true)
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Intent alarmIntent = new Intent(this, OnAlarmReceiver.class).putExtra(ACTION, ACTION_NONE);
    PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(this, 0, alarmIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    alarmManager.cancel(pendingAlarmIntent);
    pendingAlarmIntent.cancel();

    // cancel an eventually exisiting location broadcast receiver
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Intent locationIntent = new Intent("at.ac.uniklu.mobile.sportal.LOCATION_UPDATE");
    PendingIntent pendingLocationIntent = PendingIntent.getBroadcast(this, 0, locationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    locationManager.removeUpdates(pendingLocationIntent);
    pendingLocationIntent.cancel();

    if (isRunning()) {
        Log.e(TAG, "COULD NOT TURN OFF");
    }
}

From source file:net.easysol.dsb.web_protector.antiphishing.AntiphishingController.java

public void stopWebAnalyzerService() {
    if (this.webAnalyzerListener != null) {
        this.webAnalyzerListener.onWebAnalyzerOff();
    }/*from   w w  w.j a va 2s  .c  om*/
    Log.i("SERVICE", "Stoping web protector");
    AlarmManager alarmManager = (AlarmManager) this.myContext
            .getSystemService(NotificationCompatApi24.CATEGORY_ALARM);
    Intent service = new Intent(this.myContext, WebAnalyzerService.class);
    PendingIntent op = PendingIntent.getService(this.myContext,
            WebProtectorPreferences.REQUEST_CODE_WEB_PROTECTOR, service, 0);
    op.cancel();
    alarmManager.cancel(op);
    this.myContext.stopService(service);
}

From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java

final static private void showMessageNotification(TaskManagerParms taskMgrParms, EnvironmentParms envParms,
        CommonUtilities util, TaskResponse tr, String m_text, int led_color, int led_on, int led_off) {
    NotificationManager nm = (NotificationManager) taskMgrParms.context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder nb = new NotificationCompat.Builder(taskMgrParms.context);
    nm.cancel("MSG", taskMgrParms.msgNotificationId);
    Intent in = new Intent();
    PendingIntent pi = PendingIntent.getActivity(taskMgrParms.context, 0, in,
            PendingIntent.FLAG_CANCEL_CURRENT);
    pi.cancel();
    nb.setContentIntent(pi).setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
            .setOngoing(false).setAutoCancel(true).setSmallIcon(R.drawable.action)
            .setContentTitle(taskMgrParms.context.getString(R.string.app_name)).setContentText(m_text)
            .setWhen(System.currentTimeMillis());
    if (led_color != 0)
        nb.setLights(led_color, led_on, led_off);
    Notification nf = nb.build();
    nm.notify("MSG", taskMgrParms.msgNotificationId, nf);
    synchronized (envParms) {
        if (taskMgrParms.msgNotificationId >= MAX_NOTIFICATION_COUNT)
            taskMgrParms.msgNotificationId = 1;
        else/*from   w  ww.j  a v a2  s.c  o m*/
            taskMgrParms.msgNotificationId++;
    }
}

From source file:me.tatarka.support.internal.job.JobStore.java

private void unmarkForBootSession(JobStatus jobStatus) {
    Intent intent = new Intent(mContext, JobSchedulerService.class).setAction(jobStatus.toShortString());
    PendingIntent pendingIntent = PendingIntent.getService(mContext, jobStatus.getJobId(), intent,
            PendingIntent.FLAG_NO_CREATE);
    if (pendingIntent != null) {
        AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pendingIntent);/*w  w  w  . jav a 2s . c o  m*/
        pendingIntent.cancel();
    }
}

From source file:com.android.deskclock.data.TimerNotificationBuilderPreN.java

@Override
public Notification build(Context context, NotificationModel nm, List<Timer> unexpired) {
    final Timer timer = unexpired.get(0);
    final long remainingTime = timer.getRemainingTime();

    // Generate some descriptive text, a title, and some actions based on timer states.
    final String contentText;
    final String contentTitle;
    @DrawableRes// ww  w .  jav  a  2 s.  com
    int firstActionIconId, secondActionIconId = 0;
    @StringRes
    int firstActionTitleId, secondActionTitleId = 0;
    Intent firstActionIntent, secondActionIntent = null;

    if (unexpired.size() == 1) {
        contentText = formatElapsedTimeUntilExpiry(context, remainingTime);

        if (timer.isRunning()) {
            // Single timer is running.
            if (TextUtils.isEmpty(timer.getLabel())) {
                contentTitle = context.getString(R.string.timer_notification_label);
            } else {
                contentTitle = timer.getLabel();
            }

            firstActionIconId = R.drawable.ic_pause_24dp;
            firstActionTitleId = R.string.timer_pause;
            firstActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

            secondActionIconId = R.drawable.ic_add_24dp;
            secondActionTitleId = R.string.timer_plus_1_min;
            secondActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_ADD_MINUTE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
        } else {
            // Single timer is paused.
            contentTitle = context.getString(R.string.timer_paused);

            firstActionIconId = R.drawable.ic_start_24dp;
            firstActionTitleId = R.string.sw_resume_button;
            firstActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_START_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

            secondActionIconId = R.drawable.ic_reset_24dp;
            secondActionTitleId = R.string.sw_reset_button;
            secondActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_RESET_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
        }
    } else {
        if (timer.isRunning()) {
            // At least one timer is running.
            final String timeRemaining = formatElapsedTimeUntilExpiry(context, remainingTime);
            contentText = context.getString(R.string.next_timer_notif, timeRemaining);
            contentTitle = context.getString(R.string.timers_in_use, unexpired.size());
        } else {
            // All timers are paused.
            contentText = context.getString(R.string.all_timers_stopped_notif);
            contentTitle = context.getString(R.string.timers_stopped, unexpired.size());
        }

        firstActionIconId = R.drawable.ic_reset_24dp;
        firstActionTitleId = R.string.timer_reset_all;
        firstActionIntent = TimerService.createResetUnexpiredTimersIntent(context);
    }

    // Intent to load the app and show the timer when the notification is tapped.
    final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_TIMERS)
            .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, R.string.label_notification);

    final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setOngoing(true)
            .setLocalOnly(true).setShowWhen(false).setAutoCancel(false).setContentText(contentText)
            .setContentTitle(contentTitle).setContentIntent(pendingShowApp)
            .setSmallIcon(R.drawable.stat_notify_timer).setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setColor(ContextCompat.getColor(context, R.color.default_background));

    final PendingIntent action1 = Utils.pendingServiceIntent(context, firstActionIntent);
    final String action1Title = context.getString(firstActionTitleId);
    builder.addAction(firstActionIconId, action1Title, action1);

    if (secondActionIntent != null) {
        final PendingIntent action2 = Utils.pendingServiceIntent(context, secondActionIntent);
        final String action2Title = context.getString(secondActionTitleId);
        builder.addAction(secondActionIconId, action2Title, action2);
    }

    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final Intent updateNotification = TimerService.createUpdateNotificationIntent(context);
    if (timer.isRunning() && remainingTime > MINUTE_IN_MILLIS) {
        // Schedule a callback to update the time-sensitive information of the running timer.
        final PendingIntent pi = PendingIntent.getService(context, 0, updateNotification,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

        final long nextMinuteChange = remainingTime % MINUTE_IN_MILLIS;
        final long triggerTime = SystemClock.elapsedRealtime() + nextMinuteChange;
        TimerModel.schedulePendingIntent(am, triggerTime, pi);
    } else {
        // Cancel the update notification callback.
        final PendingIntent pi = PendingIntent.getService(context, 0, updateNotification,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);
        if (pi != null) {
            am.cancel(pi);
            pi.cancel();
        }
    }

    return builder.build();
}

From source file:com.androidinspain.deskclock.data.TimerModel.java

/**
 * Updates the callback given to this application from the {@link AlarmManager} that signals the
 * expiration of the next timer. If no timers are currently set to expire (i.e. no running
 * timers exist) then this method clears the expiration callback from AlarmManager.
 *//*  w  ww  .j a  va  2  s .  c  o  m*/
private void updateAlarmManager() {
    // Locate the next firing timer if one exists.
    Timer nextExpiringTimer = null;
    for (Timer timer : getMutableTimers()) {
        if (timer.isRunning()) {
            if (nextExpiringTimer == null) {
                nextExpiringTimer = timer;
            } else if (timer.getExpirationTime() < nextExpiringTimer.getExpirationTime()) {
                nextExpiringTimer = timer;
            }
        }
    }

    // Build the intent that signals the timer expiration.
    final Intent intent = TimerService.createTimerExpiredIntent(mContext, nextExpiringTimer);

    if (nextExpiringTimer == null) {
        // Cancel the existing timer expiration callback.
        final PendingIntent pi = PendingIntent.getService(mContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);
        if (pi != null) {
            mAlarmManager.cancel(pi);
            pi.cancel();
        }
    } else {
        // Update the existing timer expiration callback.
        final PendingIntent pi = PendingIntent.getService(mContext, 0, intent,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
        schedulePendingIntent(mAlarmManager, nextExpiringTimer.getExpirationTime(), pi);
    }
}

From source file:com.wizardsofm.deskclock.data.TimerNotificationBuilderPreN.java

@Override
public Notification build(Context context, NotificationModel nm, List<Timer> unexpired) {
    final Timer timer = unexpired.get(0);
    final long remainingTime = timer.getRemainingTime();

    // Generate some descriptive text, a title, and some actions based on timer states.
    final String contentText;
    final String contentTitle;
    @DrawableRes//from   w  w  w . j a va  2 s .  com
    int firstActionIconId, secondActionIconId = 0;
    @StringRes
    int firstActionTitleId, secondActionTitleId = 0;
    Intent firstActionIntent, secondActionIntent = null;

    if (unexpired.size() == 1) {
        contentText = formatElapsedTimeUntilExpiry(context, remainingTime);

        if (timer.isRunning()) {
            // Single timer is running.
            if (TextUtils.isEmpty(timer.getLabel())) {
                contentTitle = context.getString(com.wizardsofm.deskclock.R.string.timer_notification_label);
            } else {
                contentTitle = timer.getLabel();
            }

            firstActionIconId = com.wizardsofm.deskclock.R.drawable.ic_pause_24dp;
            firstActionTitleId = com.wizardsofm.deskclock.R.string.timer_pause;
            firstActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

            secondActionIconId = com.wizardsofm.deskclock.R.drawable.ic_add_24dp;
            secondActionTitleId = com.wizardsofm.deskclock.R.string.timer_plus_1_min;
            secondActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_ADD_MINUTE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
        } else {
            // Single timer is paused.
            contentTitle = context.getString(com.wizardsofm.deskclock.R.string.timer_paused);

            firstActionIconId = com.wizardsofm.deskclock.R.drawable.ic_start_24dp;
            firstActionTitleId = com.wizardsofm.deskclock.R.string.sw_resume_button;
            firstActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_START_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

            secondActionIconId = com.wizardsofm.deskclock.R.drawable.ic_reset_24dp;
            secondActionTitleId = com.wizardsofm.deskclock.R.string.sw_reset_button;
            secondActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_RESET_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
        }
    } else {
        if (timer.isRunning()) {
            // At least one timer is running.
            final String timeRemaining = formatElapsedTimeUntilExpiry(context, remainingTime);
            contentText = context.getString(com.wizardsofm.deskclock.R.string.next_timer_notif, timeRemaining);
            contentTitle = context.getString(com.wizardsofm.deskclock.R.string.timers_in_use, unexpired.size());
        } else {
            // All timers are paused.
            contentText = context.getString(com.wizardsofm.deskclock.R.string.all_timers_stopped_notif);
            contentTitle = context.getString(com.wizardsofm.deskclock.R.string.timers_stopped,
                    unexpired.size());
        }

        firstActionIconId = com.wizardsofm.deskclock.R.drawable.ic_reset_24dp;
        firstActionTitleId = com.wizardsofm.deskclock.R.string.timer_reset_all;
        firstActionIntent = TimerService.createResetUnexpiredTimersIntent(context);
    }

    // Intent to load the app and show the timer when the notification is tapped.
    final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_TIMERS)
            .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL,
                    com.wizardsofm.deskclock.R.string.label_notification);

    final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setOngoing(true)
            .setLocalOnly(true).setShowWhen(false).setAutoCancel(false).setContentText(contentText)
            .setContentTitle(contentTitle).setContentIntent(pendingShowApp)
            .setSmallIcon(com.wizardsofm.deskclock.R.drawable.stat_notify_timer)
            .setPriority(NotificationCompat.PRIORITY_HIGH).setCategory(NotificationCompat.CATEGORY_ALARM)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setColor(ContextCompat.getColor(context, com.wizardsofm.deskclock.R.color.default_background));

    final PendingIntent action1 = Utils.pendingServiceIntent(context, firstActionIntent);
    final String action1Title = context.getString(firstActionTitleId);
    builder.addAction(firstActionIconId, action1Title, action1);

    if (secondActionIntent != null) {
        final PendingIntent action2 = Utils.pendingServiceIntent(context, secondActionIntent);
        final String action2Title = context.getString(secondActionTitleId);
        builder.addAction(secondActionIconId, action2Title, action2);
    }

    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final Intent updateNotification = TimerService.createUpdateNotificationIntent(context);
    if (timer.isRunning() && remainingTime > MINUTE_IN_MILLIS) {
        // Schedule a callback to update the time-sensitive information of the running timer.
        final PendingIntent pi = PendingIntent.getService(context, 0, updateNotification,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

        final long nextMinuteChange = remainingTime % MINUTE_IN_MILLIS;
        final long triggerTime = SystemClock.elapsedRealtime() + nextMinuteChange;
        TimerModel.schedulePendingIntent(am, triggerTime, pi);
    } else {
        // Cancel the update notification callback.
        final PendingIntent pi = PendingIntent.getService(context, 0, updateNotification,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);
        if (pi != null) {
            am.cancel(pi);
            pi.cancel();
        }
    }

    return builder.build();
}