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:com.csipsimple.service.SipNotifications.java

public void showNotificationForVoiceMail(SipProfile acc, int numberOfMessages) {
    if (messageVoicemail == null) {

        messageVoicemail = new NotificationCompat.Builder(context);
        messageVoicemail.setSmallIcon(android.R.drawable.stat_notify_voicemail);
        messageVoicemail.setTicker(context.getString(R.string.voice_mail));
        messageVoicemail.setWhen(System.currentTimeMillis());
        messageVoicemail.setDefaults(Notification.DEFAULT_ALL);
        messageVoicemail.setAutoCancel(true);
        messageVoicemail.setOnlyAlertOnce(true);
    }//  w  w  w.  j a v a 2s  . co m

    PendingIntent contentIntent = null;
    Intent notificationIntent;
    if (acc != null && !TextUtils.isEmpty(acc.vm_nbr) && acc.vm_nbr != "null") {
        notificationIntent = new Intent(Intent.ACTION_CALL);
        notificationIntent.setData(
                SipUri.forgeSipUri(SipManager.PROTOCOL_CSIP, acc.vm_nbr + "@" + acc.getDefaultDomain()));
        notificationIntent.putExtra(SipProfile.FIELD_ACC_ID, acc.id);
    } else {
        notificationIntent = new Intent(SipManager.ACTION_SIP_DIALER);
    }
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    String messageText = "";
    if (acc != null) {
        messageText += acc.getProfileName() + " : ";
    }
    messageText += Integer.toString(numberOfMessages);

    messageVoicemail.setContentTitle(context.getString(R.string.voice_mail));
    messageVoicemail.setContentText(messageText);
    if (contentIntent != null) {
        messageVoicemail.setContentIntent(contentIntent);
        notificationManager.notify(VOICEMAIL_NOTIF_ID, messageVoicemail.build());
    }
}

From source file:com.stanleyidesis.quotograph.api.controller.LWQNotificationControllerImpl.java

@Override
public void postSurveyNotification() {
    LWQApplication lwqApplication = LWQApplication.get();

    // Prepare PIs
    final String responseAction = lwqApplication.getString(R.string.action_survey_response);
    Intent neverIntent = new Intent(responseAction);
    neverIntent.setClass(lwqApplication, LWQReceiver.class); // I HATE YOU, PENDING INTENT
    neverIntent.setData(Uri.parse(String.valueOf(UserSurveyController.RESPONSE_NEVER)));
    Intent laterIntent = new Intent(responseAction);
    laterIntent.setData(Uri.parse(String.valueOf(UserSurveyController.RESPONSE_LATER)));
    laterIntent.setClass(lwqApplication, LWQReceiver.class);
    Intent okayIntent = new Intent(responseAction);
    okayIntent.setData(Uri.parse(String.valueOf(UserSurveyController.RESPONSE_OKAY)));
    okayIntent.setClass(lwqApplication, LWQReceiver.class);
    PendingIntent neverPI = PendingIntent.getBroadcast(lwqApplication, uniqueRequestCode++, neverIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    PendingIntent laterPI = PendingIntent.getBroadcast(lwqApplication, uniqueRequestCode++, laterIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    PendingIntent okayPI = PendingIntent.getBroadcast(lwqApplication, uniqueRequestCode++, okayIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(lwqApplication);
    notificationBuilder.setAutoCancel(true)
            .addAction(R.drawable.md_transparent, lwqApplication.getString(R.string.survey_never), neverPI)
            .addAction(R.drawable.md_transparent, lwqApplication.getString(R.string.survey_later), laterPI)
            .addAction(R.drawable.md_transparent, lwqApplication.getString(R.string.survey_okay), okayPI)
            .setCategory(Notification.CATEGORY_PROMO)
            .setColor(lwqApplication.getResources().getColor(R.color.palette_A100)).setContentIntent(okayPI)
            .setContentText(lwqApplication.getString(R.string.survey_cta))
            .setContentTitle(lwqApplication.getString(R.string.survey_title)).setDeleteIntent(laterPI)
            .setLights(lwqApplication.getResources().getColor(R.color.palette_A100), 500, 500).setOngoing(false)
            .setPriority(NotificationCompat.PRIORITY_MAX).setShowWhen(false).setSmallIcon(R.mipmap.ic_stat)
            .setTicker(lwqApplication.getString(R.string.survey_title)).setWhen(System.currentTimeMillis());

    NotificationManager notificationManager = (NotificationManager) LWQApplication.get()
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIF_ID_SURVEY, notificationBuilder.build());
}

From source file:org.ohmage.triggers.types.location.LocTrigService.java

private void setTriggerAlwaysAlarm(int trigId, String trigDesc) {

    cancelTriggerAlwaysAlarm(trigId);/*from w  w  w .  j  a v a 2  s  .c  o  m*/

    LocTrigDesc desc = new LocTrigDesc();
    if (!desc.loadString(trigDesc)) {
        return;
    }

    if (!desc.shouldTriggerAlways()) {
        return;
    }

    Log.i(DEBUG_TAG, "LocTrigService: Setting trigger always alarm(" + trigId + ", " + trigDesc + ")");

    Calendar target = Calendar.getInstance();
    target.set(Calendar.HOUR_OF_DAY, desc.getEndTime().getHour());
    target.set(Calendar.MINUTE, desc.getEndTime().getMinute());
    target.set(Calendar.SECOND, 0);

    LocationTrigger locTrig = new LocationTrigger();
    if (locTrig.hasTriggeredToday(this, trigId)) {
        target.add(Calendar.DAY_OF_YEAR, 1);
    } else if (System.currentTimeMillis() >= target.getTimeInMillis()) {
        target.add(Calendar.DAY_OF_YEAR, 1);
    }

    Intent i = createTriggerAlwaysAlarmIntent(trigId);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager alarmMan = (AlarmManager) getSystemService(ALARM_SERVICE);

    Log.i(DEBUG_TAG, "LocTrigService: Calculated target time: " + target.getTime().toString());

    long alarmTime = target.getTimeInMillis();

    /* Convert the alarm time to elapsed real time. 
     * If we dont do this, a time change in the system might
     * set off all the alarms and a trigger might go off before
     * we get a chance to cancel it
     */
    long elapsedRT = alarmTime - System.currentTimeMillis();
    if (elapsedRT <= 0) {
        Log.i(DEBUG_TAG, "LocTrigService: negative elapsed realtime - " + "alarm not setting: " + trigId);
        return;
    }

    Log.i(DEBUG_TAG, "LocTrigService: Setting alarm for " + elapsedRT + " millis into the future");

    alarmMan.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + elapsedRT, pi);
}

From source file:com.sip.pwc.sipphone.service.SipNotifications.java

public void showNotificationForVoiceMail(SipProfile acc, int numberOfMessages) {
    if (messageVoicemail == null) {

        messageVoicemail = new NotificationCompat.Builder(context);
        messageVoicemail.setSmallIcon(android.R.drawable.stat_notify_voicemail);
        messageVoicemail.setTicker(context.getString(R.string.voice_mail));
        messageVoicemail.setWhen(System.currentTimeMillis());
        messageVoicemail.setDefaults(Notification.DEFAULT_ALL);
        messageVoicemail.setAutoCancel(true);
        messageVoicemail.setOnlyAlertOnce(true);
    }//from   www  . jav a 2 s  .  c o m

    PendingIntent contentIntent = null;
    Intent notificationIntent;
    if (acc != null && !TextUtils.isEmpty(acc.vm_nbr) && acc.vm_nbr != "null") {
        notificationIntent = new Intent(Intent.ACTION_CALL);
        notificationIntent.setData(
                SipUri.forgeSipUri(SipManager.PROTOCOL_CSIP, acc.vm_nbr + "@" + acc.getDefaultDomain()));
        notificationIntent.putExtra(SipProfile.FIELD_ACC_ID, acc.id);
    } else {
        notificationIntent = new Intent(SipManager.ACTION_SIP_DIALER);
    }
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    String messageText = "";
    if (acc != null) {
        messageText += acc.getProfileName();
        if (numberOfMessages > 0) {
            messageText += " : ";
        }
    }
    if (numberOfMessages > 0) {
        messageText += Integer.toString(numberOfMessages);
    }

    messageVoicemail.setContentTitle(context.getString(R.string.voice_mail));
    messageVoicemail.setContentText(messageText);
    if (contentIntent != null) {
        messageVoicemail.setContentIntent(contentIntent);
        notificationManager.notify(VOICEMAIL_NOTIF_ID, messageVoicemail.build());
    }
}

From source file:cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.receiver.NotificationHelper.java

/**
 * Schedules to be woken up at the next notification time.
 *//*from  w  w  w.j  ava  2  s  . co  m*/
private static void scheduleNext(Context context) {
    // Get first future notification
    final Calendar now = Calendar.getInstance();
    final List<cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.model.sql.Notification> notifications = cn.studyjams.s2.sj0132.bowenyan.mygirlfriend.nononsenseapps.notepad.data.model.sql.Notification
            .getNotificationsWithTime(context, now.getTimeInMillis(), false);

    // if not empty, schedule alarm wake up
    if (!notifications.isEmpty()) {
        // at first's time
        // Create a new PendingIntent and add it to the AlarmManager
        Intent intent = new Intent(Intent.ACTION_RUN);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pendingIntent);
        am.set(AlarmManager.RTC_WAKEUP, notifications.get(0).time, pendingIntent);
    }

    monitorUri(context);
}

From source file:org.mozilla.gecko.updater.UpdateService.java

private void showDownloadNotification(File downloadFile) {

    Intent notificationIntent = new Intent(UpdateServiceHelper.ACTION_APPLY_UPDATE);
    notificationIntent.setClass(this, UpdateService.class);

    Intent cancelIntent = new Intent(UpdateServiceHelper.ACTION_CANCEL_DOWNLOAD);
    cancelIntent.setClass(this, UpdateService.class);

    if (downloadFile != null)
        notificationIntent.putExtra(UpdateServiceHelper.EXTRA_PACKAGE_PATH_NAME,
                downloadFile.getAbsolutePath());

    PendingIntent contentIntent = PendingIntent.getService(this, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent deleteIntent = PendingIntent.getService(this, 0, cancelIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setContentTitle(getResources().getString(R.string.updater_downloading_title))
            .setContentText(/*  ww  w. jav  a2 s.  com*/
                    mApplyImmediately ? "" : getResources().getString(R.string.updater_downloading_select))
            .setSmallIcon(android.R.drawable.stat_sys_download).setContentIntent(contentIntent)
            .setDeleteIntent(deleteIntent);

    mBuilder.setProgress(100, 0, true);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

From source file:org.smssecure.smssecure.notifications.MessageNotifier.java

private static void scheduleReminder(Context context, int count) {
    if (count >= SilencePreferences.getRepeatAlertsCount(context)) {
        return;/*from  w  w w. j  ava  2  s. com*/
    }

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent alarmIntent = new Intent(ReminderReceiver.REMINDER_ACTION);
    alarmIntent.putExtra("reminder_count", count);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    long timeout = TimeUnit.MINUTES.toMillis(2);

    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeout, pendingIntent);
}

From source file:com.sonetel.service.SipNotifications.java

public void showNotificationForMissedCall(ContentValues callLog) {
    int icon = android.R.drawable.stat_notify_missed_call;
    CharSequence tickerText = context.getText(R.string.missed_call);
    long when = System.currentTimeMillis();

    if (missedCallNotification == null) {
        missedCallNotification = new NotificationCompat.Builder(context);
        missedCallNotification.setSmallIcon(icon);
        missedCallNotification.setTicker(tickerText);
        missedCallNotification.setWhen(when);
        missedCallNotification.setOnlyAlertOnce(true);
        missedCallNotification.setAutoCancel(true);
        missedCallNotification.setDefaults(Notification.DEFAULT_ALL);
    }/*ww w .  ja v  a2s  .  c  o  m*/

    Intent notificationIntent = new Intent(SipManager.ACTION_SIP_CALLLOG);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    String remoteContact = callLog.getAsString(CallLog.Calls.NUMBER);
    long accId = callLog.getAsLong(SipManager.CALLLOG_PROFILE_ID_FIELD);
    missedCallNotification.setContentTitle(formatNotificationTitle(R.string.missed_call, accId));
    missedCallNotification.setContentText(formatRemoteContactString(remoteContact));
    missedCallNotification.setContentIntent(contentIntent);

    notificationManager.notify(CALLLOG_NOTIF_ID, missedCallNotification.getNotification());
}

From source file:opensource.zeocompanion.ZeoCompanionApplication.java

private void configAlarmManagerToPrefs() {
    // setup a daily alarm if auto-emailing is enabled
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean enabledAutoEmail = prefs.getBoolean("email_auto_enable", false);
    boolean enabledDatabaseReplicate = prefs.getBoolean("database_replicate_zeo", false);
    long desiredTOD = prefs.getLong("email_auto_send_time", 0); // will default to midnight
    long configuredTOD = prefs.getLong("email_auto_send_time_configured", 0); // will default to midnight

    // determine whether there is an active AlarmManager entry that we have established
    AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
    Intent intentCheck = new Intent(this, ZeoCompanionApplication.AlarmReceiver.class);
    intentCheck.setAction(ZeoCompanionApplication.ACTION_ALARMMGR_WAKEUP_RTC);
    PendingIntent existingPi = PendingIntent.getBroadcast(this, 0, intentCheck, PendingIntent.FLAG_NO_CREATE);

    if (enabledAutoEmail || enabledDatabaseReplicate) {
        // Daily AlarmManager is needed
        if (existingPi != null && desiredTOD != configuredTOD) {
            // there is an existing AlarmManager entry, but it has the incorrect starting time-of-day;
            // so cancel it, and rebuild a new one
            Intent intent1 = new Intent(this, ZeoCompanionApplication.AlarmReceiver.class);
            intent1.setAction(ZeoCompanionApplication.ACTION_ALARMMGR_WAKEUP_RTC);
            PendingIntent pi1 = PendingIntent.getBroadcast(this, 0, intent1, PendingIntent.FLAG_CANCEL_CURRENT);
            am.cancel(pi1);//from  w  ww . j av  a  2 s  .  com
            pi1.cancel();
            existingPi = null;
        }
        if (existingPi == null) {
            // there is no existing AlarmManager entry, so create it
            Date dt = new Date(desiredTOD);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, dt.getHours());
            calendar.set(Calendar.MINUTE, dt.getMinutes());
            calendar.set(Calendar.SECOND, dt.getSeconds());
            Intent intent2 = new Intent(this, ZeoCompanionApplication.AlarmReceiver.class);
            intent2.setAction(ZeoCompanionApplication.ACTION_ALARMMGR_WAKEUP_RTC);
            PendingIntent pi2 = PendingIntent.getBroadcast(this, 0, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
            am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                    pi2);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putLong("email_auto_send_time_configured", desiredTOD);
            editor.commit();
        }

    } else {
        // Daily AlarmManager is not needed
        if (existingPi != null) {
            // there is an AlarmManager entry pending; need to cancel it
            Intent intent3 = new Intent(this, ZeoCompanionApplication.AlarmReceiver.class);
            intent3.setAction(ZeoCompanionApplication.ACTION_ALARMMGR_WAKEUP_RTC);
            PendingIntent pi3 = PendingIntent.getBroadcast(this, 0, intent3, PendingIntent.FLAG_CANCEL_CURRENT);
            am.cancel(pi3);
            pi3.cancel();
        }
    }
}

From source file:org.smssecure.smssecure.notifications.MessageNotifier.java

public static void clearReminder(Context context) {
    Intent alarmIntent = new Intent(ReminderReceiver.REMINDER_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);//from  ww w  .j  a v  a2  s .c o  m
}