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:info.tongrenlu.MediaNotificationManager.java

public MediaNotificationManager(MusicService service) {
    mService = service;//  ww  w .j  a  va  2s .co m

    mNotificationColor = Color.DKGRAY;
    mNotificationManager = (NotificationManager) mService.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent togglePlaybackIntent = new Intent(mService, MusicService.class);
    togglePlaybackIntent.setAction(MusicService.CMD_TOGGLE_PLAYBACK);
    mTogglePlaybackPendingIntent = PendingIntent.getService(mService, REQUEST_CODE, togglePlaybackIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Intent previousIntent = new Intent(mService, MusicService.class);
    previousIntent.setAction(MusicService.CMD_PREV);
    mPreviousPendingIntent = PendingIntent.getService(mService, REQUEST_CODE, previousIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Intent nextIntent = new Intent(mService, MusicService.class);
    nextIntent.setAction(MusicService.CMD_NEXT);
    mNextPendingIntent = PendingIntent.getService(mService, REQUEST_CODE, nextIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    Intent deleteIntent = new Intent(mService, MusicService.class);
    nextIntent.setAction(MusicService.CMD_STOP);
    mDeletePendingIntent = PendingIntent.getBroadcast(mService, REQUEST_CODE, deleteIntent,
            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:org.pixmob.droidlink.service.DeviceInitService.java

@Override
public void onCreate() {
    super.onCreate();
    prefs = getSharedPreferences(SHARED_PREFERENCES_FILE, MODE_PRIVATE);
    prefsEditor = prefs.edit();// w w  w.  ja v a 2  s. c o  m
    openMainActivity = PendingIntent.getActivity(this, 0, new Intent(this, EventsActivity.class),
            PendingIntent.FLAG_CANCEL_CURRENT);
}

From source file:com.flowzr.budget.holo.export.flowzr.FlowzrBillTask.java

protected Object work(Context context, DatabaseAdapter dba, String... params) throws ImportExportException {

    AccountManager accountManager = AccountManager.get(context);
    android.accounts.Account[] accounts = accountManager.getAccountsByType("com.google");

    String accountName = MyPreferences.getFlowzrAccount(context);
    if (accountName == null) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, FlowzrSyncActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        Builder mNotifyBuilder = new NotificationCompat.Builder(context);
        mNotifyBuilder.setContentIntent(contentIntent).setSmallIcon(R.drawable.icon)
                .setWhen(System.currentTimeMillis()).setAutoCancel(true)
                .setContentTitle(context.getString(R.string.flowzr_sync))
                .setContentText(context.getString(R.string.flowzr_choose_account));
        nm.notify(0, mNotifyBuilder.build());
        Log.i("Flowzr", "account name is null");
        throw new ImportExportException(R.string.flowzr_choose_account);
    }/*from ww w  . j  av  a 2s  .co m*/
    Account useCredential = null;
    for (int i = 0; i < accounts.length; i++) {
        if (accountName.equals(((android.accounts.Account) accounts[i]).name)) {
            useCredential = accounts[i];
        }
    }
    AccountManager.get(context).getAuthToken(useCredential, "ah", null, (Activity) context,
            new GetAuthTokenCallback(), null);

    return null;
}

From source file:com.cfc.needblood.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *//*from ww  w.  j  a va2  s  .c  om*/
private static void generateNotification(Context context, String message) {
    long when = System.currentTimeMillis();

    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

    // Change the icons to conform Android's design guildeline
    builder.setContentIntent(contentIntent).setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.bd_icon))
            .setTicker(message).setWhen(when).setAutoCancel(true)
            .setContentTitle(context.getString(R.string.app_name))
            .setContentText(context.getString(R.string.notification_message));

    Notification notification = builder.build();

    notificationManager.notify(0, notification);

    // Notification sound, comment out if do not need
    Uri ringtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone r = RingtoneManager.getRingtone(context, ringtone);
    r.play();
}

From source file:com.geecko.QuickLyric.broadcastReceiver.WearableRequestReceiver.java

@Override
public void onLyricsDownloaded(Lyrics lyrics) {
    if (lyrics.isLRC()) {
        LrcView lrcView = new LrcView(mContext, null);
        lrcView.setOriginalLyrics(lyrics);
        lrcView.setSourceLrc(lyrics.getText());
        lyrics.setText(lrcView.getStaticLyrics().getText());
    }/*from w  w  w .ja v a  2 s.  com*/

    NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(mContext);
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mContext);

    Intent activityIntent = new Intent("com.geecko.QuickLyric.getLyrics").putExtra("TAGS",
            new String[] { lyrics.getArtist(), lyrics.getTitle() });
    PendingIntent openAction = PendingIntent.getActivity(mContext, 0, activityIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    BigTextStyle bigStyle = new BigTextStyle();
    bigStyle.bigText(lyrics.getText() != null ? Html.fromHtml(lyrics.getText()) : "");

    int[] themes = new int[] { R.style.Theme_QuickLyric, R.style.Theme_QuickLyric_Red,
            R.style.Theme_QuickLyric_Purple, R.style.Theme_QuickLyric_Indigo, R.style.Theme_QuickLyric_Green,
            R.style.Theme_QuickLyric_Lime, R.style.Theme_QuickLyric_Brown, R.style.Theme_QuickLyric_Dark };
    int themeNum = Integer.valueOf(sharedPref.getString("pref_theme", "0"));
    int notificationPref = Integer.valueOf(sharedPref.getString("pref_notifications", "0"));

    TypedValue primaryColorValue = new TypedValue();
    mContext.setTheme(themes[themeNum]);
    mContext.getTheme().resolveAttribute(R.attr.colorPrimary, primaryColorValue, true);

    notifBuilder.setSmallIcon(R.drawable.ic_notif).setContentTitle(mContext.getString(R.string.app_name))
            .setContentText(String.format("%s - %s", lyrics.getArtist(), lyrics.getTitle())).setStyle(bigStyle)
            .setGroup("Lyrics_Notification").setOngoing(false).setColor(primaryColorValue.data)
            .setGroupSummary(false).setContentIntent(openAction).setVisibility(-1); // Notification.VISIBILITY_SECRET

    if (notificationPref == 2)
        notifBuilder.setPriority(-2);

    if (lyrics.getFlag() < 0)
        notifBuilder.extend(new NotificationCompat.WearableExtender().setContentIntentAvailableOffline(false));

    Notification notif = notifBuilder.build();

    NotificationManagerCompat.from(mContext).notify(8, notif);
}

From source file:com.google.android.apps.dashclock.PeriodicExtensionRefreshReceiver.java

/**
 * Cancels the refresh schedule if one isn't set already.
 *///from  w  w  w . j av  a  2s .  c o m
protected static void cancelPeriodicRefresh(final Context context) {
    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0,
            new Intent(context, PeriodicExtensionRefreshReceiver.class).setAction(ACTION_PERIODIC_ALARM),
            PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(pi);
}

From source file:br.ajmarques.cordova.plugin.localnotification.LocalNotification.java

/**
 * Set an alarm./*w  w w  . j a va 2s . c  om*/
 *
 * @param options
 *            The options that can be specified per alarm.
 */
public static void add(Options options) {
    long triggerTime = options.getDate();

    Intent intent = new Intent(context, Receiver.class).setAction("" + options.getId())
            .putExtra(Receiver.OPTIONS, options.getJSONObject().toString());

    AlarmManager am = getAlarmManager();
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

    am.set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
}

From source file:com.cyanogenmod.account.util.CMAccountUtils.java

public static void scheduleRetry(Context context, SharedPreferences prefs, Intent intent) {
    int backoffTimeMs = getBackoff(prefs);
    int nextAttempt = backoffTimeMs / 2 + sRandom.nextInt(backoffTimeMs);
    if (CMAccount.DEBUG)
        Log.d(TAG, "Scheduling retry, backoff = " + nextAttempt + " (" + backoffTimeMs + ") for "
                + intent.getAction());//from  w w w.j  av a  2 s. c o m
    PendingIntent retryPendingIntent = PendingIntent.getService(context, 0, intent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + nextAttempt, retryPendingIntent);
    if (backoffTimeMs < CMAccount.MAX_BACKOFF_MS) {
        setBackoff(prefs, backoffTimeMs * 2);
    }
}

From source file:org.pixmob.freemobile.netstat.SyncService.java

public static void schedule(Context context, boolean enabled) {
    final Context appContext = context.getApplicationContext();
    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent syncIntent = PendingIntent.getService(appContext, 0,
            new Intent(appContext, SyncService.class), PendingIntent.FLAG_CANCEL_CURRENT);
    am.cancel(syncIntent);// w  w w.  j ava2  s .  c  o  m

    if (enabled) {
        // Set the sync period.
        long period = AlarmManager.INTERVAL_HOUR;
        final int syncErrors = context.getSharedPreferences(INTERNAL_SP_NAME, MODE_PRIVATE)
                .getInt(INTERNAL_SP_KEY_SYNC_ERRORS, 0);
        if (syncErrors != 0) {
            // When there was a sync error, the sync period is longer.
            period = AlarmManager.INTERVAL_HOUR * Math.min(syncErrors, MAX_SYNC_ERRORS);
        }

        // Add a random time to prevent concurrent requests for the server.
        final long fuzz = RANDOM.nextInt(1000 * 60 * 30);
        period += fuzz;

        if (DEBUG) {
            Log.d(TAG, "Scheduling synchronization: next in " + (period / 1000 / 60) + " minutes");
        }
        final long syncTime = System.currentTimeMillis() + period;
        am.set(AlarmManager.RTC_WAKEUP, syncTime, syncIntent);
    } else {
        if (DEBUG) {
            Log.d(TAG, "Synchronization schedule canceled");
        }
    }
}

From source file:com.ayogo.cordova.notification.ScheduledNotificationManager.java

public ScheduledNotification scheduleNotification(String title, JSONObject options) {
    LOG.v(NotificationPlugin.TAG, "scheduleNotification: " + title);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    ScheduledNotification notification = new ScheduledNotification(title, options);

    long alarmTime = notification.at;

    if (alarmTime != 0) { //0 = uninitialized.

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

        LOG.v(NotificationPlugin.TAG, "create Intent: " + notification.tag);

        Intent intent = new Intent(context, TriggerReceiver.class);
        intent.setAction(notification.tag);

        PendingIntent pi = PendingIntent.getBroadcast(context, INTENT_REQUEST_CODE, intent,
                PendingIntent.FLAG_CANCEL_CURRENT);

        LOG.v(NotificationPlugin.TAG, "schedule alarm for: " + alarmTime);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pi);
        } else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pi);
        }
    } else {
        // No "at", trigger the notification right now.
        showNotification(notification);
    }

    return notification;
}