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:mdn.vtvsport.GcmIntentService.java

private static void generateNotification(Context context, GCMInfo info) {
    /*// w w  w  . java 2 s  .  co m
    int icon = R.drawable.icon_small;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
    .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, info.getMessage(),
    when);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context,
    BaseSlideMenuActivity.class);
    notificationIntent.putExtra(GCMBUNDLE_ID, info.getItemId());
    notificationIntent.putExtra(GCMBUNDLE_TYPE, info.getType());
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
    | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, 0,
    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, title, info.getMessage(),
    intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
    */

    String message = info.getMessage();
    Intent notificationIntent = new Intent(context, BaseSlideMenuActivity.class);
    notificationIntent.putExtra(GCMBUNDLE_ID, info.getItemId());
    notificationIntent.putExtra(GCMBUNDLE_TYPE, info.getType());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//FLAG_ACTIVITY_NEW_TASK

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(BaseSlideMenuActivity.class);
    stackBuilder.addNextIntent(notificationIntent);
    int id = -1;
    try {
        id = Integer.parseInt(info.getMessageId());
    } catch (Exception e) {
        // TODO: handle exception
    }
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(id, PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setTicker(message)
            .setSmallIcon(R.drawable.icon_small).setContentTitle(HomeFragment.APP_NAME_1)
            .setContentText(message).setAutoCancel(true).setContentIntent(resultPendingIntent)
            .setStyle(new NotificationCompat.BigTextStyle().setSummaryText(message));

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_AUTO_CANCEL;
    mNotificationManager.notify(id, notification);
}

From source file:net.xisberto.work_schedule.alarm.AlarmMessageActivity.java

private void showNotification() {
    Intent alarmIntent = new Intent(this, AlarmMessageActivity.class);
    alarmIntent.setAction(AlarmMessageActivity.ACTION_SHOW_ALARM);
    alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

    PendingIntent alarmSender = PendingIntent.getActivity(this, period.getId(), alarmIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Intent dismissIntent = new Intent(this, AlarmMessageActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).setAction(ACTION_DISMISS_ALARM);
    PendingIntent dismissSender = PendingIntent.getActivity(this, REQ_DISMISS, dismissIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Intent snoozeIntent = new Intent(this, AlarmMessageActivity.class)
            .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).setAction(ACTION_SNOOZE_ALARM);
    PendingIntent snoozeSender = PendingIntent.getActivity(this, REQ_SNOOZE, snoozeIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_notification).setContentTitle(this.getString(period.getLabelId()))
            .setTicker(this.getString(period.getLabelId())).setWhen(period.time.getTimeInMillis())
            .setOngoing(false).setOnlyAlertOnce(true).setContentIntent(alarmSender)
            .setDeleteIntent(dismissSender)
            .addAction(R.drawable.ic_snooze, getString(R.string.snooze), snoozeSender)
            .addAction(R.drawable.ic_dismiss, getString(R.string.dismiss), dismissSender).build();

    nm.notify(period.getId(), notification);
}

From source file:info.tongrenlu.MediaNotificationManager.java

private PendingIntent createContentIntent() {
    Intent openUI = new Intent(mService, FullScreenPlayerActivity.class);
    openUI.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    return PendingIntent.getActivity(mService, REQUEST_CODE, openUI, PendingIntent.FLAG_CANCEL_CURRENT);
}

From source file:com.google.android.apps.iosched.calendar.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);/*from w w w . java2 s  .  c om*/
    final long currentTime = System.currentTimeMillis();

    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart) {
        return;
    }

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - TEN_MINUTES_MILLIS;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    final Intent alarmIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);

    // Setting data to ensure intent's uniqueness for different session start times.
    alarmIntent.setData(new Uri.Builder().authority(ScheduleContract.CONTENT_AUTHORITY)
            .path(String.valueOf(sessionStart)).build());
    alarmIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    alarmIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    alarmIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    am.set(AlarmManager.RTC_WAKEUP, alarmTime,
            PendingIntent.getService(this, 0, alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT));
}

From source file:co.vanir.indecentxposure.IndecentXposure.java

public static void notify(final Context context, final String exampleString) {
    final Resources res = context.getResources();

    final String ticker = exampleString;
    final String title = res.getString(R.string.solve_problems_notification_title); //_template, exampleString);
    final String text = res.getString(R.string.solve_problems_notification_placeholder_text_template);//, exampleString);

    //one button opens the "uninstall app" settings page
    final Intent removeIntent = new Intent();
    removeIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
    Uri uri = Uri.fromParts("package", SerialOffender.getPackageName(), null);
    removeIntent.setData(uri);//ww  w .  j a  v a2 s. c om

    //the other option will hide the notification until the user uninstalls and reinstalls
    //  the installer
    final Intent ignoreIntent = new Intent();
    ignoreIntent.setAction("co.vanir.indecentxposure.IGNORE_LIKELY_FUNK");

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_stat_solve_problems).setContentTitle(title).setContentText(text)
            .setPriority(NotificationCompat.PRIORITY_MAX).setTicker(ticker)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(text).setBigContentTitle(title)
                    .setSummaryText(res.getString(R.string.summary_thanks)))
            .addAction(0, res.getString(R.string.action_remove),
                    PendingIntent.getActivity(context, 0, removeIntent, PendingIntent.FLAG_UPDATE_CURRENT))
            .addAction(0, res.getString(R.string.action_accept_consequences),
                    PendingIntent.getBroadcast(context, 0, ignoreIntent, PendingIntent.FLAG_CANCEL_CURRENT));

    notify(context, builder.build(), true);
}

From source file:ch.fixme.status.Widget.java

protected static void updateWidget(final Context ctxt, int widgetId, AppWidgetManager manager, Bitmap bitmap,
        String text) {/*from w  w  w. ja  v  a  2s.  c o  m*/
    RemoteViews views = new RemoteViews(ctxt.getPackageName(), R.layout.widget);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctxt);
    Editor edit = prefs.edit();
    if (prefs.getBoolean(Prefs.KEY_WIDGET_TRANSPARENCY, Prefs.DEFAULT_WIDGET_TRANSPARENCY)) {
        views.setInt(R.id.widget_image, "setBackgroundResource", 0);
    } else {
        views.setInt(R.id.widget_image, "setBackgroundResource", android.R.drawable.btn_default_small);
    }
    if (bitmap != null) {
        views.setImageViewBitmap(R.id.widget_image, bitmap);
        edit.putBoolean(Main.PREF_FORCE_WIDGET + widgetId, false); // Don't
        // need
        // to
        // force
    } else {
        views.setImageViewResource(R.id.widget_image, android.R.drawable.ic_popup_sync);
        edit.putBoolean(Main.PREF_FORCE_WIDGET + widgetId, true); // Something
        // went
        // wrong
    }
    if (text != null) {
        views.setTextViewText(R.id.widget_status, text);
        views.setViewVisibility(R.id.widget_status, View.VISIBLE);
    } else {
        views.setViewVisibility(R.id.widget_status, View.GONE);
    }
    Intent clickIntent = new Intent(ctxt, Main.class);
    clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctxt, widgetId, clickIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    views.setOnClickPendingIntent(R.id.widget_image, pendingIntent);
    manager.updateAppWidget(widgetId, views);
    // Is initialized
    edit.putBoolean(Main.PREF_INIT_WIDGET + widgetId, true);
    edit.commit();
}

From source file:de.appplant.cordova.plugin.localnotification.Receiver.java

/**
 * Adds an onclick handler to the notification
 *//*from ww  w. j  a  va2 s  .  co m*/
private Builder setClickEvent(Builder notification) {
    Intent intent = new Intent(context, ReceiverActivity.class)
            .putExtra(OPTIONS, options.getJSONObject().toString()).setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

    int requestCode = new Random().nextInt();

    PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    return notification.setContentIntent(contentIntent);
}

From source file:com.morlunk.mumbleclient.service.PlumbleNotification.java

/**
 * Called to update/create the service's foreground Plumble notification.
 *//*from ww w  . j  a v  a  2 s  .  c om*/
private Notification createNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(mService);
    builder.setSmallIcon(R.drawable.ic_stat_notify);
    builder.setTicker(mCustomTicker);
    builder.setContentTitle(mService.getString(R.string.app_name));
    builder.setContentText(mCustomContentText);
    builder.setPriority(NotificationCompat.PRIORITY_HIGH);
    builder.setOngoing(true);

    if (mActionsShown) {
        // Add notification triggers
        Intent muteIntent = new Intent(BROADCAST_MUTE);
        Intent deafenIntent = new Intent(BROADCAST_DEAFEN);
        Intent overlayIntent = new Intent(BROADCAST_OVERLAY);

        builder.addAction(R.drawable.ic_action_microphone, mService.getString(R.string.mute),
                PendingIntent.getBroadcast(mService, 1, muteIntent, PendingIntent.FLAG_CANCEL_CURRENT));
        builder.addAction(R.drawable.ic_action_audio, mService.getString(R.string.deafen),
                PendingIntent.getBroadcast(mService, 1, deafenIntent, PendingIntent.FLAG_CANCEL_CURRENT));
        builder.addAction(R.drawable.ic_action_channels, mService.getString(R.string.overlay),
                PendingIntent.getBroadcast(mService, 2, overlayIntent, PendingIntent.FLAG_CANCEL_CURRENT));
    }

    // Show unread messages
    if (mMessages.size() > 0) {
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        for (String message : mMessages) {
            inboxStyle.addLine(message);
        }
        builder.setStyle(inboxStyle);
    }

    Intent channelListIntent = new Intent(mService, PlumbleActivity.class);
    channelListIntent.putExtra(PlumbleActivity.EXTRA_DRAWER_FRAGMENT, DrawerAdapter.ITEM_SERVER);
    // FLAG_CANCEL_CURRENT ensures that the extra always gets sent.
    PendingIntent pendingIntent = PendingIntent.getActivity(mService, 0, channelListIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(pendingIntent);

    Notification notification = builder.build();
    mService.startForeground(NOTIFICATION_ID, notification);
    return notification;
}

From source file:com.google.android.apps.iosched.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);//  w w w  . j av a 2 s  .co  m
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart)
        return;

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.google.android.apps.iosched")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}

From source file:com.gdgdevfest.android.apps.devfestbcn.service.SessionAlarmService.java

private void scheduleAlarm(final long sessionStart, final long sessionEnd, final long alarmOffset) {

    NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(NOTIFICATION_ID);//from   w w w  .  j  ava  2 s.co  m
    final long currentTime = UIUtils.getCurrentTime(this);
    // If the session is already started, do not schedule system notification.
    if (currentTime > sessionStart)
        return;

    // By default, sets alarm to go off at 10 minutes before session start time.  If alarm
    // offset is provided, alarm is set to go off by that much time from now.
    long alarmTime;
    if (alarmOffset == UNDEFINED_ALARM_OFFSET) {
        alarmTime = sessionStart - MILLI_TEN_MINUTES;
    } else {
        alarmTime = currentTime + alarmOffset;
    }

    final Intent notifIntent = new Intent(ACTION_NOTIFY_SESSION, null, this, SessionAlarmService.class);
    // Setting data to ensure intent's uniqueness for different session start times.
    notifIntent.setData(new Uri.Builder().authority("com.gdgdevfest.android.apps.devfestbcn")
            .path(String.valueOf(sessionStart)).build());
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_START, sessionStart);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_END, sessionEnd);
    notifIntent.putExtra(SessionAlarmService.EXTRA_SESSION_ALARM_OFFSET, alarmOffset);
    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    final AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    // Schedule an alarm to be fired to notify user of added sessions are about to begin.
    am.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
}