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:org.catrobat.catroid.utils.StatusBarNotificationManager.java

public int createUploadNotification(Context context, String programName) {
    if (context == null || programName == null) {
        return -1;
    }/*from w  ww  . ja va 2  s  . c  o  m*/
    initNotificationManager(context);

    Intent uploadIntent = new Intent(context, MainMenuActivity.class);
    uploadIntent.setAction(Intent.ACTION_MAIN);
    uploadIntent = uploadIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationId, uploadIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationData data = new NotificationData(context, pendingIntent, R.drawable.ic_stat, programName,
            R.string.notification_upload_title_pending, R.string.notification_upload_title_finished,
            R.string.notification_upload_pending, R.string.notification_upload_finished);

    int id = createNotification(context, data);
    showOrUpdateNotification(id, 0);
    return id;
}

From source file:ca.hoogit.hooold.Scheduling.Sms.java

public void single(Context context, SmsManager manager, String recipient, int count) {
    Intent sent = generateIntent(Consts.INTENT_SMS_SENT, recipient, count);
    PendingIntent pendingSent = PendingIntent.getBroadcast(context, (int) id, sent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Intent delivered = generateIntent(Consts.INTENT_SMS_DELIVERED, recipient, count);
    PendingIntent pendingDelivered = PendingIntent.getBroadcast(context, (int) id, delivered,
            PendingIntent.FLAG_CANCEL_CURRENT);

    manager.sendTextMessage(recipient, null, messageBody, pendingSent, pendingDelivered);
}

From source file:com.example.android.supportv4.media.MediaNotificationManager.java

public MediaNotificationManager(MediaBrowserServiceSupport service) {
    mService = service;//from w w  w. ja v a  2s  . c  om
    updateSessionToken();

    mNotificationColor = ResourceHelper.getThemeColor(mService, android.R.attr.colorPrimary, Color.DKGRAY);

    mNotificationManager = NotificationManagerCompat.from(mService);

    String pkg = mService.getPackageName();
    mPauseIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, new Intent(ACTION_PAUSE).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mPlayIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, new Intent(ACTION_PLAY).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mPreviousIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE,
            new Intent(ACTION_PREV).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT);
    mNextIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, new Intent(ACTION_NEXT).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);

    // Cancel all notifications to handle the case where the Service was killed and
    // restarted by the system.
    mNotificationManager.cancelAll();
}

From source file:com.android.shell.BugreportReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
    final File screenshotFile = getFileExtra(intent, EXTRA_SCREENSHOT);

    // Files are kept on private storage, so turn into Uris that we can
    // grant temporary permissions for.
    final Uri bugreportUri = FileProvider.getUriForFile(context, AUTHORITY, bugreportFile);
    final Uri screenshotUri = FileProvider.getUriForFile(context, AUTHORITY, screenshotFile);

    Intent sendIntent = buildSendIntent(context, bugreportUri, screenshotUri);
    Intent notifIntent;/*  w  ww .ja  v a 2s . co  m*/

    // Send through warning dialog by default
    if (getWarningState(context, STATE_SHOW) == STATE_SHOW) {
        notifIntent = buildWarningIntent(context, sendIntent);
    } else {
        notifIntent = sendIntent;
    }
    notifIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    final Notification.Builder builder = new Notification.Builder(context);
    builder.setSmallIcon(com.android.internal.R.drawable.stat_sys_adb);
    builder.setContentTitle(context.getString(R.string.bugreport_finished_title));
    builder.setTicker(context.getString(R.string.bugreport_finished_title));
    builder.setContentText(context.getString(R.string.bugreport_finished_text));
    builder.setContentIntent(
            PendingIntent.getActivity(context, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT));
    builder.setAutoCancel(true);
    NotificationManager.from(context).notify(TAG, 0, builder.build());

    // Clean up older bugreports in background
    final PendingResult result = goAsync();
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            FileUtils.deleteOlderFiles(bugreportFile.getParentFile(), MIN_KEEP_COUNT, MIN_KEEP_AGE);
            result.finish();
            return null;
        }
    }.execute();
}

From source file:com.google.android.apps.iosched.gcm.GCMIntentService.java

@Override
protected void onMessage(Context context, Intent intent) {
    if (UIUtils.isGoogleTV(context)) {
        // Google TV uses SyncHelper directly.
        return;//  w  ww . ja v  a 2 s  . com
    }
    String announcement = intent.getStringExtra("announcement");
    if (announcement != null) {
        displayNotification(context, announcement);
        return;
    }

    int jitterMillis = (int) (sRandom.nextFloat() * TRIGGER_SYNC_MAX_JITTER_MILLIS);
    final String debugMessage = "Received message to trigger sync; " + "jitter = " + jitterMillis + "ms";
    LOGI(TAG, debugMessage);

    if (BuildConfig.DEBUG) {
        displayNotification(context, debugMessage);
    }

    ((AlarmManager) context.getSystemService(ALARM_SERVICE)).set(AlarmManager.RTC,
            System.currentTimeMillis() + jitterMillis, PendingIntent.getBroadcast(context, 0,
                    new Intent(context, TriggerSyncReceiver.class), PendingIntent.FLAG_CANCEL_CURRENT));
}

From source file:org.chromium.ChromeAlarms.java

private void create(final CordovaArgs args, final CallbackContext callbackContext) {
    try {/*  w w w. j  av a2s  .  co  m*/
        String name = args.getString(0);
        Alarm alarm = new Alarm(name, (long) args.getDouble(1), (long) (args.optDouble(2) * 60000));
        PendingIntent alarmPendingIntent = makePendingIntentForAlarm(name, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.cancel(alarmPendingIntent);
        if (alarm.periodInMillis == 0) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.scheduledTime, alarmPendingIntent);
        } else {
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarm.scheduledTime, alarm.periodInMillis,
                    alarmPendingIntent);
        }
        callbackContext.success();
    } catch (Exception e) {
        Log.e(LOG_TAG, "Could not create alarm", e);
        callbackContext.error("Could not create alarm");
    }
}

From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java

public void displayQuickSilenceNotification(int quicksilenceDurationMinutes) {
    BetaPrefs betaPrefs = new BetaPrefs(context);
    if (betaPrefs.getDisableNotifications()) {
        Log.d(TAG, "asked to display quicksilence notification, but this has been disabled in "
                + "beta settings. Stopping any ongoing notifications");
        cancelAllNotifications();//from  w  w w .j a  v  a 2  s .c om
    } else {
        // the intent attached to the notification should only cancel the quick silence request, but
        // not launch the app
        Intent notificationIntent = new Intent(context, EventSilencerService.class);
        notificationIntent.putExtra(EventSilencerService.WAKE_REASON, RequestTypes.CANCEL_QUICKSILENCE.value);
        long endTime = System.currentTimeMillis()
                + (long) quicksilenceDurationMinutes * EventInstance.MILLISECONDS_IN_MINUTE;

        DateFormat dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
        Date date = new Date(endTime);
        String formattedDate = dateFormat.format(date);
        String formattedString = String
                .format(context.getString(R.string.notification_quicksilence_description), formattedDate);

        // FLAG_CANCEL_CURRENT is required to make sure that the extras are including in the new
        // pending intent
        PendingIntent pendIntent = PendingIntent.getService(context, REQUEST_CODES.QUICKSILENCE_CANCEL,
                notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        //an intent to open the main app
        Intent openMainIntent = new Intent(context, MainActivity.class);
        openMainIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        openMainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent openMainPendIntent = PendingIntent.getActivity(context,
                REQUEST_CODES.QUICKSILENCE_OPEN_MAIN, openMainIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        //An additional button to extend the quicksilence by 15min
        // this intent will be attached to the button on the notification
        Intent addMinutesIntent = new Intent(context, ExtendQuicksilenceService.class);
        addMinutesIntent.putExtra(ExtendQuicksilenceService.ORIGINAL_END_TIMESTAMP, endTime);
        addMinutesIntent.putExtra(ExtendQuicksilenceService.EXTEND_LENGTH, 15);
        PendingIntent addMinPendIntent = PendingIntent.getService(context, REQUEST_CODES.QUICKSILENCE_ADD_TIME,
                addMinutesIntent, PendingIntent.FLAG_CANCEL_CURRENT);

        NotificationCompat.Builder notBuilder = new NotificationCompat.Builder(context)
                .setContentTitle(context.getString(R.string.notification_quicksilence_title))
                .setContentText(formattedString)
                .setTicker(context.getString(R.string.notification_quicksilence_ticker))
                .setSmallIcon(R.drawable.not_silence).setAutoCancel(true).setOngoing(true)
                .setContentIntent(pendIntent).setColor(context.getResources().getColor(R.color.accent))
                .setTicker(context.getString(R.string.notification_quicksilence_ticker));

        notBuilder.addAction(R.drawable.app_mono_small, "Open Tacere", openMainPendIntent);
        //the +15 button should only be available in the Pro version
        Authenticator authenticator = new Authenticator(context);
        if (authenticator.isAuthenticated()) {
            notBuilder.addAction(R.drawable.not_clock, "+15", addMinPendIntent);
        }

        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(NOTIFICATION_ID);
        nm.notify(NOTIFICATION_ID, notBuilder.build());
    }
}

From source file:de.appplant.cordova.plugin.notification.NotificationBuilder.java

/**
 * Adds an onclick handler to the notification
 *///from w  w  w .j a  v  a  2 s . c  om
private Builder setClickEvent(Builder notification) {
    Intent intent = new Intent(context, receiverActivity).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:nuclei.media.MediaNotificationManager.java

public MediaNotificationManager(MediaService service) throws RemoteException {
    mService = service;//from  w  w w . j a va 2 s  .  c om
    updateSessionToken();

    mNotificationColor = ViewUtil.getThemeAttrColor(mService, R.attr.colorPrimary);

    mNotificationManager = NotificationManagerCompat.from(service);

    String pkg = mService.getPackageName();
    mPauseIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, new Intent(ACTION_PAUSE).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mPlayIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, new Intent(ACTION_PLAY).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mPreviousIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE,
            new Intent(ACTION_PREV).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT);
    mNextIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, new Intent(ACTION_NEXT).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mStopCastIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE,
            new Intent(ACTION_STOP_CASTING).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT);
    mCancelIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE,
            new Intent(ACTION_CANCEL).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT);

    // Cancel all notifications to handle the case where the Service was killed and
    // restarted by the system.
    mNotificationManager.cancelAll();
}

From source file:audio.lisn.service.MediaNotificationManager.java

public MediaNotificationManager(AudioPlayerService service) {
    mService = service;// w  w  w  .j  ava  2  s. c o m
    Log.v("Media", "MediaNotificationManager start");

    // mController = new MediaController(mService.getApplicationContext());

    // updateSessionToken();

    mNotificationColor = mService.getResources().getColor(R.color.colorPrimary);

    //ResourceHelper.getThemeColor(mService,android.R.attr.colorPrimary, Color.DKGRAY);

    mNotificationManager = (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);

    String pkg = mService.getPackageName();
    mPauseIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, new Intent(ACTION_PAUSE).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mPlayIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, new Intent(ACTION_PLAY).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mPreviousIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE,
            new Intent(ACTION_PREV).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT);
    mNextIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, new Intent(ACTION_NEXT).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    mDeleteIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE,
            new Intent(ACTION_DELETE).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT);

    // Cancel all notifications to handle the case where the Service was killed and
    // restarted by the system.
    mNotificationManager.cancelAll();
}