Example usage for android.app PendingIntent FLAG_UPDATE_CURRENT

List of usage examples for android.app PendingIntent FLAG_UPDATE_CURRENT

Introduction

In this page you can find the example usage for android.app PendingIntent FLAG_UPDATE_CURRENT.

Prototype

int FLAG_UPDATE_CURRENT

To view the source code for android.app PendingIntent FLAG_UPDATE_CURRENT.

Click Source Link

Document

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent.

Usage

From source file:ca.rmen.android.poetassistant.wotd.Wotd.java

private static PendingIntent getShareIntent(Context context, DictionaryEntry entry) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, buildWotdShareContent(context, entry));
    intent.setType("text/plain");
    return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

From source file:ca.zadrox.dota2esportticker.service.ReminderAlarmService.java

private void scheduleAlarm(final long matchId, final long matchStart) {

    final long currentTime = TimeUtils.getUTCTime();
    LOGD(TAG, "Schedule Alarm");

    if (currentTime > matchStart) {
        LOGD(TAG, "CurrentTime > matchStart");
        return;/*  w  ww  .  ja  va 2  s  .co  m*/
    }

    long alarmTime = matchStart - MILLI_TEN_MINUTES;

    final Intent notifIntent = new Intent(ACTION_NOTIFY_MATCH, null, this, ReminderAlarmService.class);

    notifIntent.setData(
            new Uri.Builder().authority("ca.zadrox.dota2esportticker").path(String.valueOf(matchId)).build());

    notifIntent.putExtra(ReminderAlarmService.EXTRA_MATCH_ID, matchId);
    notifIntent.putExtra(ReminderAlarmService.EXTRA_MATCH_START, matchStart);

    PendingIntent pi = PendingIntent.getService(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    final AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);

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

From source file:com.arifin.taxi.penumpang.GCMIntentService.java

/**
 * Issues a notification to inform the user that server has sent a message.
 *///from  ww w  . j av  a  2  s  .  c o m
private void generateNotification(Context context, String message) {
    // System.out.println("this is message " + message);
    // System.out.println("NOTIFICATION RECEIVED!!!!!!!!!!!!!!" + message);
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, MainDrawerActivity.class);
    notificationIntent.putExtra("fromNotification", "notification");
    // 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);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    notification = builder.setContentIntent(intent).setSmallIcon(icon).setTicker(message).setWhen(when)
            .setAutoCancel(true).setContentTitle(title).setContentText(message).build();
    // mNM.notify(NOTIFICATION, notification);

    /*notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;*/

    System.out.println("notification====>" + message);
    notification.defaults |= Notification.DEFAULT_SOUND;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    // notification.defaults |= Notification.DEFAULT_LIGHTS;
    notification.flags |= Notification.FLAG_SHOW_LIGHTS;
    notification.ledARGB = 0x00000000;
    notification.ledOnMS = 0;
    notification.ledOffMS = 0;
    notificationManager.notify(0, notification);
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(
            PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,
            "WakeLock");
    wakeLock.acquire();
    wakeLock.release();
}

From source file:com.android.deskclock.data.TimerNotificationBuilderPreN.java

@Override
public Notification build(Context context, NotificationModel nm, List<Timer> unexpired) {
    final Timer timer = unexpired.get(0);
    final long remainingTime = timer.getRemainingTime();

    // Generate some descriptive text, a title, and some actions based on timer states.
    final String contentText;
    final String contentTitle;
    @DrawableRes/*from w  ww .  j a v  a  2  s  . com*/
    int firstActionIconId, secondActionIconId = 0;
    @StringRes
    int firstActionTitleId, secondActionTitleId = 0;
    Intent firstActionIntent, secondActionIntent = null;

    if (unexpired.size() == 1) {
        contentText = formatElapsedTimeUntilExpiry(context, remainingTime);

        if (timer.isRunning()) {
            // Single timer is running.
            if (TextUtils.isEmpty(timer.getLabel())) {
                contentTitle = context.getString(R.string.timer_notification_label);
            } else {
                contentTitle = timer.getLabel();
            }

            firstActionIconId = R.drawable.ic_pause_24dp;
            firstActionTitleId = R.string.timer_pause;
            firstActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_PAUSE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

            secondActionIconId = R.drawable.ic_add_24dp;
            secondActionTitleId = R.string.timer_plus_1_min;
            secondActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_ADD_MINUTE_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
        } else {
            // Single timer is paused.
            contentTitle = context.getString(R.string.timer_paused);

            firstActionIconId = R.drawable.ic_start_24dp;
            firstActionTitleId = R.string.sw_resume_button;
            firstActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_START_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());

            secondActionIconId = R.drawable.ic_reset_24dp;
            secondActionTitleId = R.string.sw_reset_button;
            secondActionIntent = new Intent(context, TimerService.class)
                    .setAction(HandleDeskClockApiCalls.ACTION_RESET_TIMER)
                    .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId());
        }
    } else {
        if (timer.isRunning()) {
            // At least one timer is running.
            final String timeRemaining = formatElapsedTimeUntilExpiry(context, remainingTime);
            contentText = context.getString(R.string.next_timer_notif, timeRemaining);
            contentTitle = context.getString(R.string.timers_in_use, unexpired.size());
        } else {
            // All timers are paused.
            contentText = context.getString(R.string.all_timers_stopped_notif);
            contentTitle = context.getString(R.string.timers_stopped, unexpired.size());
        }

        firstActionIconId = R.drawable.ic_reset_24dp;
        firstActionTitleId = R.string.timer_reset_all;
        firstActionIntent = TimerService.createResetUnexpiredTimersIntent(context);
    }

    // Intent to load the app and show the timer when the notification is tapped.
    final Intent showApp = new Intent(context, HandleDeskClockApiCalls.class)
            .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).setAction(HandleDeskClockApiCalls.ACTION_SHOW_TIMERS)
            .putExtra(HandleDeskClockApiCalls.EXTRA_TIMER_ID, timer.getId())
            .putExtra(HandleDeskClockApiCalls.EXTRA_EVENT_LABEL, R.string.label_notification);

    final PendingIntent pendingShowApp = PendingIntent.getActivity(context, 0, showApp,
            PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setOngoing(true)
            .setLocalOnly(true).setShowWhen(false).setAutoCancel(false).setContentText(contentText)
            .setContentTitle(contentTitle).setContentIntent(pendingShowApp)
            .setSmallIcon(R.drawable.stat_notify_timer).setPriority(NotificationCompat.PRIORITY_HIGH)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setColor(ContextCompat.getColor(context, R.color.default_background));

    final PendingIntent action1 = Utils.pendingServiceIntent(context, firstActionIntent);
    final String action1Title = context.getString(firstActionTitleId);
    builder.addAction(firstActionIconId, action1Title, action1);

    if (secondActionIntent != null) {
        final PendingIntent action2 = Utils.pendingServiceIntent(context, secondActionIntent);
        final String action2Title = context.getString(secondActionTitleId);
        builder.addAction(secondActionIconId, action2Title, action2);
    }

    final AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    final Intent updateNotification = TimerService.createUpdateNotificationIntent(context);
    if (timer.isRunning() && remainingTime > MINUTE_IN_MILLIS) {
        // Schedule a callback to update the time-sensitive information of the running timer.
        final PendingIntent pi = PendingIntent.getService(context, 0, updateNotification,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);

        final long nextMinuteChange = remainingTime % MINUTE_IN_MILLIS;
        final long triggerTime = SystemClock.elapsedRealtime() + nextMinuteChange;
        TimerModel.schedulePendingIntent(am, triggerTime, pi);
    } else {
        // Cancel the update notification callback.
        final PendingIntent pi = PendingIntent.getService(context, 0, updateNotification,
                PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_NO_CREATE);
        if (pi != null) {
            am.cancel(pi);
            pi.cancel();
        }
    }

    return builder.build();
}

From source file:com.android.mail.widget.WidgetService.java

public static void configureValidWidgetIntents(Context context, RemoteViews remoteViews, int appWidgetId,
        Account account, final int folderType, final int folderCapabilities, final Uri folderUri,
        final Uri folderConversationListUri, final String folderDisplayName, Class<?> serviceClass) {
    remoteViews.setViewVisibility(R.id.widget_configuration, View.GONE);

    // Launch an intent to avoid ANRs
    final Intent intent = new Intent(context, serviceClass);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.putExtra(Utils.EXTRA_ACCOUNT, account.serialize());
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_TYPE, folderType);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_CAPABILITIES, folderCapabilities);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_URI, folderUri);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_CONVERSATION_LIST_URI, folderConversationListUri);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_DISPLAY_NAME, folderDisplayName);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(R.id.conversation_list, intent);
    // Open mail app when click on header
    final Intent mailIntent = Utils.createViewFolderIntent(context, folderUri, account);
    mailIntent.setPackage(context.getPackageName());
    PendingIntent clickIntent = PendingIntent.getActivity(context, 0, mailIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);

    // On click intent for Compose
    final Intent composeIntent = new Intent();
    composeIntent.setPackage(context.getPackageName());
    composeIntent.setAction(Intent.ACTION_SEND);
    composeIntent.putExtra(Utils.EXTRA_ACCOUNT, account.serialize());
    composeIntent.setData(account.composeIntentUri);
    composeIntent.putExtra(ComposeActivity.EXTRA_FROM_EMAIL_TASK, true);
    if (account.composeIntentUri != null) {
        composeIntent.putExtra(Utils.EXTRA_COMPOSE_URI, account.composeIntentUri);
    }//from w  ww.  j  a v  a2 s  . c  o  m

    // Build a task stack that forces the conversation list on the stack before the compose
    // activity.
    final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    clickIntent = taskStackBuilder.addNextIntent(mailIntent).addNextIntent(composeIntent).getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);

    // On click intent for Conversation
    final Intent conversationIntent = new Intent();
    conversationIntent.setPackage(context.getPackageName());
    conversationIntent.setAction(Intent.ACTION_VIEW);
    clickIntent = PendingIntent.getActivity(context, 0, conversationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setPendingIntentTemplate(R.id.conversation_list, clickIntent);
}

From source file:com.upnext.blekit.actions.BLEAction.java

/**
 * Displays a notification//from  w  ww  . j a v  a  2 s  .  c  o m
 *
 * @param context Android Context, passed from the calling entity
 * @param title title for the notification
 * @param msg message and ticker for the notification
 * @param type action type that will be put as an extra into the result Intent (<code>resultIntent.putExtra("type", type);</code>)
 * @param notificationIconResId notification icon resource id
 * @param notificationId an identifier for this notification as in {@link android.app.NotificationManager#notify(int, android.app.Notification)}
 */
public static void displayNotification(Context context, String title, String msg, String type,
        int notificationIconResId, int notificationId) {
    L.d(".");

    if (BLEKit.getTargetActivityForNotifications() == null) {
        throw new IllegalArgumentException(
                "Target activity for notifications is not set. Call BLEKit.getTargetActivityForNotifications() first.");
    }

    Notification.Builder builder = new Notification.Builder(context).setContentText(msg).setTicker(msg)
            .setContentTitle(title).setAutoCancel(true).setSmallIcon(notificationIconResId);

    Intent resultIntent = new Intent(context, BLEKit.getTargetActivityForNotifications());
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    resultIntent.putExtra("type", type);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(resultPendingIntent);

    NotificationManager mNotificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(notificationId, builder.build());
}

From source file:com.chess.genesis.net.GenesisNotifier.java

private void SendNotification(final int id, final String text) {
    final Intent intent;
    if (Pref.getBool(this, R.array.pf_tabletMode)) {
        intent = new Intent(this, MainMenuTablet.class);

        final Bundle bundle = new Bundle();
        bundle.putInt("loadFrag", Enums.ONLINE_LIST);
        intent.putExtras(bundle);/*w  ww  . j  a  v a 2s  . c  om*/
    } else {
        intent = new Intent(this, GameListOnline.class);
    }

    final PendingIntent pintent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    final Notification note = createNotification(id, text, pintent);

    nm.notify(id, note);
}

From source file:com.chen.mail.widget.WidgetService.java

public static void configureValidWidgetIntents(Context context, RemoteViews remoteViews, int appWidgetId,
        Account account, final int folderType, final Uri folderUri, final Uri folderConversationListUri,
        final String folderDisplayName, Class<?> serviceClass) {
    remoteViews.setViewVisibility(R.id.widget_configuration, View.GONE);

    // Launch an intent to avoid ANRs
    final Intent intent = new Intent(context, serviceClass);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.putExtra(Utils.EXTRA_ACCOUNT, account.serialize());
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_TYPE, folderType);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_URI, folderUri);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_CONVERSATION_LIST_URI, folderConversationListUri);
    intent.putExtra(BaseWidgetProvider.EXTRA_FOLDER_DISPLAY_NAME, folderDisplayName);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    remoteViews.setRemoteAdapter(R.id.conversation_list, intent);
    // Open mail app when click on header
    final Intent mailIntent = Utils.createViewFolderIntent(context, folderUri, account);
    PendingIntent clickIntent = PendingIntent.getActivity(context, 0, mailIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_header, clickIntent);

    // On click intent for Compose
    final Intent composeIntent = new Intent();
    composeIntent.setAction(Intent.ACTION_SEND);
    composeIntent.putExtra(Utils.EXTRA_ACCOUNT, account.serialize());
    composeIntent.setData(account.composeIntentUri);
    composeIntent.putExtra(ComposeActivity.EXTRA_FROM_EMAIL_TASK, true);
    if (account.composeIntentUri != null) {
        composeIntent.putExtra(Utils.EXTRA_COMPOSE_URI, account.composeIntentUri);
    }// w  ww .j a v a 2 s.  co m

    // Build a task stack that forces the conversation list on the stack before the compose
    // activity.
    final TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    clickIntent = taskStackBuilder.addNextIntent(mailIntent).addNextIntent(composeIntent).getPendingIntent(0,
            PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setOnClickPendingIntent(R.id.widget_compose, clickIntent);

    // On click intent for Conversation
    final Intent conversationIntent = new Intent();
    conversationIntent.setAction(Intent.ACTION_VIEW);
    clickIntent = PendingIntent.getActivity(context, 0, conversationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    remoteViews.setPendingIntentTemplate(R.id.conversation_list, clickIntent);
}

From source file:com.magnet.mmx.client.MMXWakeupIntentService.java

private void invokeNotificationForPush(GCMPayload payload) {
    // Launch the activity with action=MAIN, category=DEFAULT.  Make sure that
    // it has DEFAULT category declared in AndroidManifest.xml intent-filter.
    PendingIntent intent = PendingIntent.getActivity(this.getApplicationContext(), 0,
            new Intent(Intent.ACTION_MAIN).setPackage(this.getPackageName())
                    .addCategory(Intent.CATEGORY_DEFAULT) // it is redundant
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    .addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED),
            PendingIntent.FLAG_UPDATE_CURRENT);
    // If title is not specified, use the app name (compatible with iOS push notification)
    String title = (payload.getTitle() == null) ? this.getApplicationInfo().name : payload.getTitle();
    Notification.Builder noteBuilder = new Notification.Builder(this).setContentIntent(intent)
            .setAutoCancel(true).setWhen(System.currentTimeMillis()).setContentTitle(title)
            .setContentText(payload.getBody());
    if (payload.getSound() != null) {
        // TODO: cannot handle custom sound yet; use notification ring tone.
        noteBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    }/*ww w. j  a v a 2s  . c  om*/
    if (payload.getIcon() != null) {
        noteBuilder.setSmallIcon(
                this.getResources().getIdentifier(payload.getIcon(), "drawable", this.getPackageName()));
    } else {
        noteBuilder.setSmallIcon(this.getApplicationInfo().icon);
    }
    if (payload.getBadge() != null) {
        noteBuilder.setNumber(payload.getBadge());
    }
    NotificationManager noteMgr = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    noteMgr.notify(sNoteId++, noteBuilder.build());
}