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:com.commontime.plugin.notification.notification.NotificationWrapper.java

/**
 * Schedule the local notification./*from   w  ww.java 2 s .co  m*/
 */
public void schedule() {
    long triggerTime = options.getTriggerTime();

    persist();

    // Intent gets called when the NotificationWrapper gets fired
    Intent intent = new Intent(context, receiver).setAction(options.getIdStr()).putExtra(Options.EXTRA,
            options.toString());

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

    if (isRepeating()) {
        getAlarmMgr().setRepeating(AlarmManager.RTC_WAKEUP, triggerTime, options.getRepeatInterval(), pi);
    } else {
        getAlarmMgr().set(AlarmManager.RTC_WAKEUP, triggerTime, pi);
    }
}

From source file:com.afrolkin.samplepushclient.GCMIntentService.java

private static void generateNotification(Context context, String message) {
    int icon = R.mipmap.ic_announcement_black_48dp;
    long when = System.currentTimeMillis();
    String title = context.getString(R.string.app_name);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, "Push Message", when);
    notification.sound = soundUri;/*from w w w  .j av  a2 s  . c  o  m*/

    Intent notificationIntent = new Intent(context, MainActivity.class);
    notificationIntent.putExtra("message", message);
    // 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_CANCEL_CURRENT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);
}

From source file:org.onebusaway.android.directions.realtime.RealtimeService.java

private boolean possibleReschedule(Bundle bundle) {

    // Delay if this trip doesn't start for at least an hour
    Date start = new TripRequestBuilder(bundle).getDateTime();
    Date queryStart = new Date(start.getTime() - OTPConstants.REALTIME_SERVICE_QUERY_WINDOW);
    boolean reschedule = new Date().before(queryStart);

    if (reschedule) {
        Log.d(TAG, "Start service at " + queryStart);
        Intent future = new Intent(OTPConstants.INTENT_START_CHECKS);
        future.putExtras(bundle);//from   w w  w  .ja  v a  2s .  c om

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, future,
                PendingIntent.FLAG_CANCEL_CURRENT);
        getAlarmManager().set(AlarmManager.RTC_WAKEUP, queryStart.getTime(), pendingIntent);
    }

    return reschedule;
}

From source file:org.sigimera.app.android.GCMIntentService.java

@Override
protected final void onMessage(final Context context, final Intent message) {
    final Intent msg = message;
    this.mainThreadHandler.post(new Runnable() {
        public void run() {
            ApplicationController controller = ApplicationController.getInstance();
            controller.init(getApplicationContext(), getSharedPreferences(Constants.PREFS_NAME, 0), null);
            String authToken = controller.getSharedPreferences().getString("auth_token", null);
            final String type = msg.getStringExtra("sig_message_type");
            if (type.equalsIgnoreCase("NEW_CRISIS")) {
                /**/*from   ww  w  .ja v a2s  .  c o m*/
                 * XXX: Blocks UI: Shift this code into a separate
                 * background thread
                 */
                Crisis crisis = PersistanceController.getInstance().getCrisis(authToken,
                        msg.getStringExtra("crisis_id"));

                StringBuffer message = new StringBuffer();
                if (crisis != null) {
                    message.append(crisis.getID());
                    message.append(" was stored successfully!");
                } else {
                    message.append("Not able to get crisis!");
                }
                Toast.makeText(getApplicationContext(), message.toString(), Toast.LENGTH_LONG).show();
            } else if (type.equalsIgnoreCase("PING")) {
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

                Intent notificationIntent = new Intent();
                PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                        notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

                Notification notification = new NotificationCompat.Builder(getApplicationContext())
                        .setContentTitle("Sigimera PING!")
                        .setContentText("Congratulations, push notifcation received!")
                        .setSmallIcon(R.drawable.sigimera_logo).setAutoCancel(true)
                        .setDefaults(Notification.DEFAULT_ALL).setContentIntent(contentIntent).build();

                mNotificationManager.notify(Constants.PING_ID, notification);
            } else if (type.equalsIgnoreCase("CRISIS_ALERT")) {
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

                /**
                 * XXX: Not working with random ID. That makes always the
                 * latest notification clickable, but not the older ones.
                 */
                int notificationID = new Random().nextInt();
                Intent notificationIntent = new Intent(getApplicationContext(), CrisisAlertActivity.class);
                notificationIntent.putExtra("notification_id", notificationID);
                notificationIntent.putExtra("crisis_id", msg.getStringExtra("crisis_id"));
                notificationIntent.putExtra("crisis_type", msg.getStringExtra("crisis_type"));
                PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                        notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);

                Notification notification = new NotificationCompat.Builder(getApplicationContext())
                        .setContentTitle("CRISIS ALERT!")
                        .setContentText("Crisis found: " + msg.getStringExtra("crisis_id"))
                        .setSmallIcon(R.drawable.alert_red).setOngoing(true).setAutoCancel(true)
                        .setDefaults(Notification.DEFAULT_ALL).setContentIntent(contentIntent).build();

                mNotificationManager.notify(notificationID, notification);
            } else if (type.equalsIgnoreCase("SHARED_CRISIS")) {
                Intent intent = new Intent(GCMIntentService.this, CrisisActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra(Constants.CRISIS_ID, msg.getStringExtra("crisis_id"));
                intent.putExtra(Constants.WINDOW_TYPE, Constants.WINDOW_TYPE_SHARED_CRISIS);
                startActivity(intent);
            } else if (type.equalsIgnoreCase("UNREGISTER_DEVICE")) {
                GCMRegistrar.unregister(ApplicationController.getInstance().getApplicationContext());
            } else if (type.equalsIgnoreCase("REFRESH")) {
                LocationUpdaterHttpHelper locUpdater = new LocationUpdaterHttpHelper();
                Location loc = LocationController.getInstance().getLastKnownLocation();
                if (loc != null) {
                    String latitude = loc.getLatitude() + "";
                    String longitude = loc.getLongitude() + "";
                    if (authToken != null) {
                        locUpdater.execute(authToken, latitude, longitude);
                    }
                } else {
                    // TODO: Notify the user that the update location flow
                    // has not worked.
                }
            }
        }
    });
}

From source file:com.scooter1556.sms.android.manager.MediaNotificationManager.java

public MediaNotificationManager(MediaService service) throws RemoteException {
    mediaService = service;/*  www  .  ja v  a2 s . com*/
    updateSessionToken();

    notificationManager = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);

    String pkg = mediaService.getPackageName();
    pauseIntent = PendingIntent.getBroadcast(mediaService, REQUEST_CODE,
            new Intent(ACTION_PAUSE).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT);
    playIntent = PendingIntent.getBroadcast(mediaService, REQUEST_CODE, new Intent(ACTION_PLAY).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    previousIntent = PendingIntent.getBroadcast(mediaService, REQUEST_CODE,
            new Intent(ACTION_PREV).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT);
    nextIntent = PendingIntent.getBroadcast(mediaService, REQUEST_CODE, new Intent(ACTION_NEXT).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    stopIntent = PendingIntent.getBroadcast(mediaService, REQUEST_CODE, new Intent(ACTION_STOP).setPackage(pkg),
            PendingIntent.FLAG_CANCEL_CURRENT);
    stopCastIntent = PendingIntent.getBroadcast(mediaService, REQUEST_CODE,
            new Intent(ACTION_STOP_CASTING).setPackage(pkg), PendingIntent.FLAG_CANCEL_CURRENT);

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

From source file:com.microsoft.office365.meetingfeedback.model.service.MyMeetingsService.java

private void sendNotificationForEvent(String id) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(android.R.drawable.ic_menu_my_calendar).setTicker("New Rating Received!")
            .setAutoCancel(true).setContentTitle("New Rating Received!")
            .setContentText("Your meeting has received a new rating. Click to view");
    Intent intent = new Intent(this, MeetingDetailActivity.class);
    intent.putExtra(MeetingDetailActivity.EVENT_ID_EXTRA, id);

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(pIntent);/*from w  ww. ja  va2 s . c o m*/
    mNotificationManager.notify(0, builder.build());
}

From source file:com.nextgis.maplibui.util.NotificationHelper.java

private static void showLocationNotification(Context context, String title, String info) {
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Intent locationSettings = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    PendingIntent intentNotify = PendingIntent.getActivity(context, 0, locationSettings,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setContentIntent(intentNotify)
            .setAutoCancel(true).setSmallIcon(R.drawable.ic_location).setContentTitle(title)
            .setContentText(info).setTicker(title);

    manager.notify(NOTIFICATION_GPS_ID, builder.build());
}

From source file:com.f2prateek.dfg.core.GenerateFrameService.java

@Override
public void doneImage(final Uri imageUri) {
    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override//  w w  w  .j a  v  a 2  s  .  c  o  m
        public void run() {
            bus.post(new Events.SingleImageProcessed(device, imageUri));
        }
    });

    // Create the intent to let the user share the image
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("image/png");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
    Intent chooserIntent = Intent.createChooser(sharingIntent, null);
    chooserIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
    notificationBuilder.addAction(R.drawable.ic_action_share, getResources().getString(R.string.share),
            PendingIntent.getActivity(this, 0, chooserIntent, PendingIntent.FLAG_CANCEL_CURRENT));

    // Create the intent to let the user rate the app
    Intent rateIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(AppConstants.MARKET_URL));
    notificationBuilder.addAction(R.drawable.ic_action_rate, getResources().getString(R.string.rate),
            PendingIntent.getActivity(this, 0, rateIntent, PendingIntent.FLAG_CANCEL_CURRENT));

    // Create the intent to show the screenshot in gallery
    Intent launchIntent = new Intent(Intent.ACTION_VIEW);
    launchIntent.setDataAndType(imageUri, "image/png");
    launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    notificationBuilder.setContentTitle(resources.getString(R.string.screenshot_saved_title))
            .setContentText(resources.getString(R.string.single_screenshot_saved, device.name()))
            .setContentIntent(PendingIntent.getActivity(this, 0, launchIntent, 0))
            .setWhen(System.currentTimeMillis()).setProgress(0, 0, false).setAutoCancel(true);

    notificationManager.notify(DFG_NOTIFICATION_ID, notificationBuilder.build());
}

From source file:org.amahi.anywhere.util.MediaNotificationManager.java

public MediaNotificationManager(AudioService service) throws RemoteException {
    mService = service;/*from  w  w  w  .  j  a va  2  s .  c o  m*/
    updateSessionToken();

    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);

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

From source file:hku.fyp14017.blencode.utils.StatusBarNotificationManager.java

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

    Intent downloadIntent = new Intent(context, MainMenuActivity.class);
    downloadIntent.setAction(Intent.ACTION_MAIN).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            .putExtra(EXTRA_PROJECT_NAME, programName);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, notificationId, downloadIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    NotificationData data = new NotificationData(context, pendingIntent,
            hku.fyp14017.blencode.R.drawable.ic_stat, programName,
            hku.fyp14017.blencode.R.string.notification_download_title_pending,
            hku.fyp14017.blencode.R.string.notification_title_open,
            hku.fyp14017.blencode.R.string.notification_download_pending,
            hku.fyp14017.blencode.R.string.notification_download_finished);

    return createNotification(context, data);
}