List of usage examples for android.app PendingIntent FLAG_CANCEL_CURRENT
int FLAG_CANCEL_CURRENT
To view the source code for android.app PendingIntent FLAG_CANCEL_CURRENT.
Click Source Link
From source file:com.google.samples.apps.iosched.service.SessionAlarmService.java
private void notifySession(final long sessionStart, final long alarmOffset) { long currentTime = UIUtils.getCurrentTime(this); final long intervalEnd = sessionStart + MILLI_TEN_MINUTES; LOGD(TAG, "Considering notifying for time interval."); LOGD(TAG, " Interval start: " + sessionStart + "=" + (new Date(sessionStart)).toString()); LOGD(TAG, " Interval end: " + intervalEnd + "=" + (new Date(intervalEnd)).toString()); LOGD(TAG, " Current time is: " + currentTime + "=" + (new Date(currentTime)).toString()); if (sessionStart < currentTime) { LOGD(TAG, "Skipping session notification (too late -- time interval already started)"); return;//from ww w. j a v a2s . c o m } if (!SettingsUtils.shouldShowSessionReminders(this)) { // skip if disabled in settings LOGD(TAG, "Skipping session notification for sessions. Disabled in settings."); return; } // Avoid repeated notifications. if (alarmOffset == UNDEFINED_ALARM_OFFSET && UIUtils.isNotificationFiredForBlock(this, ScheduleContract.Blocks.generateBlockId(sessionStart, intervalEnd))) { LOGD(TAG, "Skipping session notification (already notified)"); return; } final ContentResolver cr = getContentResolver(); LOGD(TAG, "Looking for sessions in interval " + sessionStart + " - " + intervalEnd); Cursor c = null; try { c = cr.query(ScheduleContract.Sessions.CONTENT_MY_SCHEDULE_URI, SessionDetailQuery.PROJECTION, ScheduleContract.Sessions.STARTING_AT_TIME_INTERVAL_SELECTION, ScheduleContract.Sessions.buildAtTimeIntervalArgs(sessionStart, intervalEnd), null); int starredCount = c.getCount(); LOGD(TAG, "# starred sessions in that interval: " + c.getCount()); String singleSessionId = null; String singleSessionRoomId = null; ArrayList<String> starredSessionTitles = new ArrayList<String>(); while (c.moveToNext()) { singleSessionId = c.getString(SessionDetailQuery.SESSION_ID); singleSessionRoomId = c.getString(SessionDetailQuery.ROOM_ID); starredSessionTitles.add(c.getString(SessionDetailQuery.SESSION_TITLE)); LOGD(TAG, "-> Title: " + c.getString(SessionDetailQuery.SESSION_TITLE)); } if (starredCount < 1) { return; } // Generates the pending intent which gets fired when the user taps on the notification. // NOTE: Use TaskStackBuilder to comply with Android's design guidelines // related to navigation from notifications. Intent baseIntent = new Intent(this, MyScheduleActivity.class); baseIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); TaskStackBuilder taskBuilder = TaskStackBuilder.create(this).addNextIntent(baseIntent); // For a single session, tapping the notification should open the session details (b/15350787) if (starredCount == 1) { taskBuilder.addNextIntent( new Intent(Intent.ACTION_VIEW, ScheduleContract.Sessions.buildSessionUri(singleSessionId))); } PendingIntent pi = taskBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT); final Resources res = getResources(); String contentText; int minutesLeft = (int) (sessionStart - currentTime + 59000) / 60000; if (minutesLeft < 1) { minutesLeft = 1; } if (starredCount == 1) { contentText = res.getString(R.string.session_notification_text_1, minutesLeft); } else { contentText = res.getQuantityString(R.plurals.session_notification_text, starredCount - 1, minutesLeft, starredCount - 1); } NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this) .setContentTitle(starredSessionTitles.get(0)).setContentText(contentText) .setColor(getResources().getColor(R.color.theme_primary)) .setTicker(res .getQuantityString(R.plurals.session_notification_ticker, starredCount, starredCount)) .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setLights(SessionAlarmService.NOTIFICATION_ARGB_COLOR, SessionAlarmService.NOTIFICATION_LED_ON_MS, SessionAlarmService.NOTIFICATION_LED_OFF_MS) .setSmallIcon(R.drawable.ic_stat_notification).setContentIntent(pi) .setPriority(Notification.PRIORITY_MAX).setAutoCancel(true); if (minutesLeft > 5) { notifBuilder.addAction(R.drawable.ic_alarm_holo_dark, String.format(res.getString(R.string.snooze_x_min), 5), createSnoozeIntent(sessionStart, intervalEnd, 5)); } if (starredCount == 1 && SettingsUtils.isAttendeeAtVenue(this)) { notifBuilder.addAction(R.drawable.ic_map_holo_dark, res.getString(R.string.title_map), createRoomMapIntent(singleSessionRoomId)); } String bigContentTitle; if (starredCount == 1 && starredSessionTitles.size() > 0) { bigContentTitle = starredSessionTitles.get(0); } else { bigContentTitle = res.getQuantityString(R.plurals.session_notification_title, starredCount, minutesLeft, starredCount); } NotificationCompat.InboxStyle richNotification = new NotificationCompat.InboxStyle(notifBuilder) .setBigContentTitle(bigContentTitle); // Adds starred sessions starting at this time block to the notification. for (int i = 0; i < starredCount; i++) { richNotification.addLine(starredSessionTitles.get(i)); } NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); LOGD(TAG, "Now showing notification."); nm.notify(NOTIFICATION_ID, richNotification.build()); } finally { if (c != null) { try { c.close(); } catch (Exception ignored) { } } } }
From source file:com.razza.apps.iosched.service.SessionAlarmService.java
private void notifySession(final long sessionStart, final long alarmOffset) { long currentTime = UIUtils.getCurrentTime(this); final long intervalEnd = sessionStart + MILLI_TEN_MINUTES; LogUtils.LOGD(TAG, "Considering notifying for time interval."); LogUtils.LOGD(TAG, " Interval start: " + sessionStart + "=" + (new Date(sessionStart)).toString()); LogUtils.LOGD(TAG, " Interval end: " + intervalEnd + "=" + (new Date(intervalEnd)).toString()); LogUtils.LOGD(TAG, " Current time is: " + currentTime + "=" + (new Date(currentTime)).toString()); if (sessionStart < currentTime) { LogUtils.LOGD(TAG, "Skipping session notification (too late -- time interval already started)"); return;/*from w w w . j a va 2s.c o m*/ } if (!SettingsUtils.shouldShowSessionReminders(this)) { // skip if disabled in settings LogUtils.LOGD(TAG, "Skipping session notification for sessions. Disabled in settings."); return; } // Avoid repeated notifications. if (alarmOffset == UNDEFINED_ALARM_OFFSET && UIUtils.isNotificationFiredForBlock(this, ScheduleContract.Blocks.generateBlockId(sessionStart, intervalEnd))) { LogUtils.LOGD(TAG, "Skipping session notification (already notified)"); return; } final ContentResolver cr = getContentResolver(); LogUtils.LOGD(TAG, "Looking for sessions in interval " + sessionStart + " - " + intervalEnd); Cursor c = null; try { c = cr.query(ScheduleContract.Sessions.CONTENT_MY_SCHEDULE_URI, SessionDetailQuery.PROJECTION, ScheduleContract.Sessions.STARTING_AT_TIME_INTERVAL_SELECTION, ScheduleContract.Sessions.buildAtTimeIntervalArgs(sessionStart, intervalEnd), null); int starredCount = c.getCount(); LogUtils.LOGD(TAG, "# starred sessions in that interval: " + c.getCount()); String singleSessionId = null; String singleSessionRoomId = null; ArrayList<String> starredSessionTitles = new ArrayList<String>(); while (c.moveToNext()) { singleSessionId = c.getString(SessionDetailQuery.SESSION_ID); singleSessionRoomId = c.getString(SessionDetailQuery.ROOM_ID); starredSessionTitles.add(c.getString(SessionDetailQuery.SESSION_TITLE)); LogUtils.LOGD(TAG, "-> Title: " + c.getString(SessionDetailQuery.SESSION_TITLE)); } if (starredCount < 1) { return; } // Generates the pending intent which gets fired when the user taps on the notification. // NOTE: Use TaskStackBuilder to comply with Android's design guidelines // related to navigation from notifications. Intent baseIntent = new Intent(this, MyScheduleActivity.class); baseIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); TaskStackBuilder taskBuilder = TaskStackBuilder.create(this).addNextIntent(baseIntent); // For a single session, tapping the notification should open the session details (b/15350787) if (starredCount == 1) { taskBuilder.addNextIntent( new Intent(Intent.ACTION_VIEW, ScheduleContract.Sessions.buildSessionUri(singleSessionId))); } PendingIntent pi = taskBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT); final Resources res = getResources(); String contentText; int minutesLeft = (int) (sessionStart - currentTime + 59000) / 60000; if (minutesLeft < 1) { minutesLeft = 1; } if (starredCount == 1) { contentText = res.getString(R.string.session_notification_text_1, minutesLeft); } else { contentText = res.getQuantityString(R.plurals.session_notification_text, starredCount - 1, minutesLeft, starredCount - 1); } NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(this) .setContentTitle(starredSessionTitles.get(0)).setContentText(contentText) .setColor(getResources().getColor(R.color.theme_primary)) .setTicker(res .getQuantityString(R.plurals.session_notification_ticker, starredCount, starredCount)) .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) .setLights(SessionAlarmService.NOTIFICATION_ARGB_COLOR, SessionAlarmService.NOTIFICATION_LED_ON_MS, SessionAlarmService.NOTIFICATION_LED_OFF_MS) .setSmallIcon(R.drawable.ic_stat_notification).setContentIntent(pi) .setPriority(Notification.PRIORITY_MAX).setAutoCancel(true); if (minutesLeft > 5) { notifBuilder.addAction(R.drawable.ic_alarm_holo_dark, String.format(res.getString(R.string.snooze_x_min), 5), createSnoozeIntent(sessionStart, intervalEnd, 5)); } if (starredCount == 1 && SettingsUtils.isAttendeeAtVenue(this)) { notifBuilder.addAction(R.drawable.ic_map_holo_dark, res.getString(R.string.title_map), createRoomMapIntent(singleSessionRoomId)); } String bigContentTitle; if (starredCount == 1 && starredSessionTitles.size() > 0) { bigContentTitle = starredSessionTitles.get(0); } else { bigContentTitle = res.getQuantityString(R.plurals.session_notification_title, starredCount, minutesLeft, starredCount); } NotificationCompat.InboxStyle richNotification = new NotificationCompat.InboxStyle(notifBuilder) .setBigContentTitle(bigContentTitle); // Adds starred sessions starting at this time block to the notification. for (int i = 0; i < starredCount; i++) { richNotification.addLine(starredSessionTitles.get(i)); } NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); LogUtils.LOGD(TAG, "Now showing notification."); nm.notify(NOTIFICATION_ID, richNotification.build()); } finally { if (c != null) { try { c.close(); } catch (Exception ignored) { } } } }
From source file:de.ub0r.android.websms.WebSMSReceiver.java
/** * Schedules resend of a message./*from w w w. ja v a 2 s . c o m*/ * * @param context context * @param specs {@link de.ub0r.android.websms.connector.common.ConnectorSpec} * @param command {@link de.ub0r.android.websms.connector.common.ConnectorCommand} */ private static void scheduleMessageResend(final Context context, final ConnectorSpec specs, final ConnectorCommand command) { long msgId = command.getMsgId(); final Intent resendIntent = new Intent(Connector.ACTION_RESEND); command.setToIntent(resendIntent); specs.setToIntent(resendIntent); AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + RESEND_DELAY_MS, PendingIntent.getBroadcast(context, (int) msgId, resendIntent, PendingIntent.FLAG_CANCEL_CURRENT)); }
From source file:com.vuze.android.remote.service.VuzeService.java
private NotificationCompat.Builder getNotificationBuilder() { Resources resources = getResources(); final Intent notificationIntent = new Intent(this, IntentHandler.class); final PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0); String title = resources.getString(R.string.core_noti_title); NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.notification_small).setContentTitle(title).setOngoing(true) .setCategory(Notification.CATEGORY_SERVICE).setContentIntent(pi); if (!isCoreStopping) { Intent intentStop = new Intent(this, VuzeService.class); intentStop.setAction(INTENT_ACTION_STOP); PendingIntent piStop = PendingIntent.getService(this, 0, intentStop, PendingIntent.FLAG_CANCEL_CURRENT); builder.addAction(R.drawable.ic_power_settings_new_white_24dp, resources.getString(R.string.core_noti_stop_button), piStop); AzureusCore core = getCore();/*from w ww.j a va2 s . c o m*/ if (core != null && core.isStarted()) { GlobalManager gm = core.getGlobalManager(); if (gm != null) { boolean canPause = gm.canPauseDownloads(); Intent intentPR = new Intent(this, VuzeService.class); intentPR.setAction(canPause ? INTENT_ACTION_PAUSE : INTENT_ACTION_RESUME); PendingIntent piPR = PendingIntent.getService(this, 0, intentPR, PendingIntent.FLAG_CANCEL_CURRENT); builder.addAction( canPause ? R.drawable.ic_playlist_pause_n : R.drawable.ic_playlist_play_white_n, resources.getString( canPause ? R.string.core_noti_pause_button : R.string.core_noti_resume_button), piPR); } } } String subTitle = null; if (isCoreStopping || isServiceStopping) { int id = restartService ? R.string.core_noti_restarting : R.string.core_noti_stopping; subTitle = resources.getString(id); } else { if (bindToLocalHost) { subTitle = resources.getString(bindToLocalHostReasonID); } else { GlobalManagerStats stats = null; AzureusCore core = getCore(); if (core != null && core.isStarted()) { GlobalManager gm = staticCore.getGlobalManager(); if (gm != null) { stats = gm.getStats(); } } if (stats != null) { String downSpeed = DisplayFormatters .formatByteCountToKiBEtcPerSec(stats.getDataAndProtocolSendRate()); String upSpeed = DisplayFormatters .formatByteCountToKiBEtcPerSec(stats.getDataAndProtocolReceiveRate()); TagManager tagManager = TagManagerFactory.getTagManager(); Tag tagActive = tagManager.lookupTagByUID(7);// active int numActive = tagActive == null ? 0 : tagActive.getTaggedCount(); subTitle = resources.getQuantityString(R.plurals.core_noti_running, numActive, downSpeed, upSpeed, DisplayFormatters.formatNumber(numActive)); } else { subTitle = resources.getString(R.string.core_noti_starting); } } } builder.setContentText(subTitle); return builder; }
From source file:com.mobiperf.MeasurementScheduler.java
@SuppressWarnings("unused") private void updateNotificationBar(String notificationMsg) { // The intent to launch when the user clicks the expanded notification Intent intent = new Intent(this, SpeedometerApp.class); PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); // This constructor is deprecated in 3.x. But most phones still run 2.x systems Notification notice = new Notification(R.drawable.icon_statusbar, notificationMsg, System.currentTimeMillis()); // This is deprecated in 3.x. But most phones still run 2.x systems notice.setLatestEventInfo(this, "Speedometer", notificationMsg, pendIntent); notificationManager.notify(NOTIFICATION_ID, notice); }
From source file:me.tatarka.support.internal.job.JobStore.java
private void markForBootSession(JobStatus jobStatus) { // Pending intents are cleared on reboot. Therefore, we can use one to mark that we haven't // rebooted yet. Intent intent = new Intent(mContext, JobSchedulerService.class).setAction(jobStatus.toShortString()); PendingIntent pendingIntent = PendingIntent.getService(mContext, jobStatus.getJobId(), intent, PendingIntent.FLAG_CANCEL_CURRENT); // Have the alarm manager hold on to our pending intent so it will still be there even if our app is killed. AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); am.set(AlarmManager.ELAPSED_REALTIME, Long.MAX_VALUE, pendingIntent); }
From source file:de.ub0r.android.smsdroid.SmsReceiver.java
/** * Update failed message notification./*from w ww .j a va2 s.c o m*/ * * @param context {@link Context} * @param uri {@link Uri} to message */ private static void updateFailedNotification(final Context context, final Uri uri) { Log.d(TAG, "updateFailedNotification: ", uri); final Cursor c = context.getContentResolver().query(uri, Message.PROJECTION_SMS, null, null, null); if (c != null && c.moveToFirst()) { final int id = c.getInt(Message.INDEX_ID); final int tid = c.getInt(Message.INDEX_THREADID); final String body = c.getString(Message.INDEX_BODY); final long date = c.getLong(Message.INDEX_DATE); Conversation conv = Conversation.getConversation(context, tid, true); final NotificationManager mNotificationMgr = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context); final boolean privateNotification = p.getBoolean(PreferencesActivity.PREFS_NOTIFICATION_PRIVACY, false); Intent intent; if (conv == null) { intent = new Intent(Intent.ACTION_VIEW, null, context, SenderActivity.class); } else { intent = new Intent(Intent.ACTION_VIEW, conv.getUri(), context, MessageListActivity.class); } intent.putExtra(Intent.EXTRA_TEXT, body); String title = context.getString(R.string.error_sending_failed); final int[] ledFlash = PreferencesActivity.getLEDflash(context); final NotificationCompat.Builder b = new NotificationCompat.Builder(context) .setSmallIcon(android.R.drawable.stat_sys_warning).setTicker(title).setWhen(date) .setAutoCancel(true).setLights(RED, ledFlash[0], ledFlash[1]).setContentIntent( PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)); String text; if (privateNotification) { title += "!"; text = ""; } else if (conv == null) { title += "!"; text = body; } else { title += ": " + conv.getContact().getDisplayName(); text = body; } b.setContentTitle(title); b.setContentText(text); final String s = p.getString(PreferencesActivity.PREFS_SOUND, null); if (!TextUtils.isEmpty(s)) { b.setSound(Uri.parse(s)); } final boolean vibrate = p.getBoolean(PreferencesActivity.PREFS_VIBRATE, false); if (vibrate) { final long[] pattern = PreferencesActivity.getVibratorPattern(context); if (pattern.length > 1) { b.setVibrate(pattern); } } mNotificationMgr.notify(id, b.build()); } if (c != null && !c.isClosed()) { c.close(); } }
From source file:com.android.mail.utils.NotificationActionUtils.java
public static Notification createUndoNotification(final Context context, final NotificationAction notificationAction, final int notificationId) { LogUtils.i(LOG_TAG, "createUndoNotification for %s", notificationAction.getNotificationActionType()); final NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.ic_notification_mail_24dp); builder.setWhen(notificationAction.getWhen()); builder.setCategory(NotificationCompat.CATEGORY_EMAIL); final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification); undoView.setTextViewText(R.id.description_text, context.getString(notificationAction.getActionTextResId())); final String packageName = context.getPackageName(); final Intent clickIntent = new Intent(NotificationActionIntentService.ACTION_UNDO); clickIntent.setPackage(packageName); clickIntent.setData(notificationAction.mConversation.uri); putNotificationActionExtra(clickIntent, notificationAction); final PendingIntent clickPendingIntent = PendingIntent.getService(context, notificationId, clickIntent, PendingIntent.FLAG_CANCEL_CURRENT); undoView.setOnClickPendingIntent(R.id.status_bar_latest_event_content, clickPendingIntent); builder.setContent(undoView);/*from w ww.j a v a 2 s.com*/ // When the notification is cleared, we perform the destructive action final Intent deleteIntent = new Intent(NotificationActionIntentService.ACTION_DESTRUCT); deleteIntent.setPackage(packageName); deleteIntent.setData(notificationAction.mConversation.uri); putNotificationActionExtra(deleteIntent, notificationAction); final PendingIntent deletePendingIntent = PendingIntent.getService(context, notificationId, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setDeleteIntent(deletePendingIntent); final Notification notification = builder.build(); return notification; }
From source file:com.google.samples.apps.sergio.service.SessionAlarmService.java
private PendingIntent createRoomMapIntent(final String roomId) { Intent mapIntent = new Intent(getApplicationContext(), UIUtils.getMapActivityClass(getApplicationContext())); mapIntent.putExtra(BaseMapActivity.EXTRA_ROOM, roomId); mapIntent.putExtra(BaseMapActivity.EXTRA_DETACHED_MODE, true); return TaskStackBuilder.create(getApplicationContext()) .addNextIntent(new Intent(this, BrowseSessionsActivity.class)).addNextIntent(mapIntent) .getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT); }
From source file:com.tct.mail.utils.NotificationActionUtils.java
public static Notification createUndoNotification(final Context context, final NotificationAction notificationAction, final int notificationId) { LogUtils.i(LOG_TAG, "createUndoNotification for %s", notificationAction.getNotificationActionType()); final NotificationCompat.Builder builder = new NotificationCompat.Builder(context); builder.setSmallIcon(R.drawable.ic_notification_mail_24dp); builder.setWhen(notificationAction.getWhen()); final RemoteViews undoView = new RemoteViews(context.getPackageName(), R.layout.undo_notification); undoView.setTextViewText(R.id.description_text, context.getString(notificationAction.getActionTextResId())); final String packageName = context.getPackageName(); final Intent clickIntent = new Intent(NotificationActionIntentService.ACTION_UNDO); clickIntent.setPackage(packageName); clickIntent.setData(notificationAction.mConversation.uri); putNotificationActionExtra(clickIntent, notificationAction); final PendingIntent clickPendingIntent = PendingIntent.getService(context, notificationId, clickIntent, PendingIntent.FLAG_CANCEL_CURRENT); undoView.setOnClickPendingIntent(R.id.status_bar_latest_event_content, clickPendingIntent); builder.setContent(undoView);// w ww .j av a 2s. c o m // When the notification is cleared, we perform the destructive action final Intent deleteIntent = new Intent(NotificationActionIntentService.ACTION_DESTRUCT); deleteIntent.setPackage(packageName); deleteIntent.setData(notificationAction.mConversation.uri); putNotificationActionExtra(deleteIntent, notificationAction); final PendingIntent deletePendingIntent = PendingIntent.getService(context, notificationId, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setDeleteIntent(deletePendingIntent); final Notification notification = builder.build(); return notification; }