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.torrenttunes.android.MediaNotificationManager.java

public MediaNotificationManager(MusicService service) {
    mService = service;//from  w  w  w .j a  va2  s.c  o m
    updateSessionToken();

    mNotificationColor = 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);
    mStopCastIntent = PendingIntent.getBroadcast(mService, 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.
    mNotificationManager.cancelAll();
}

From source file:com.turkiyedenemeleri.chronometer.ForegroundService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(Constants.ACTION.STARTFOREGROUND_ACTION)) {

        PrefUtils mPreferences = new PrefUtils(this);
        long wakeUpTime = (mPreferences.getStartedTime(intent.getStringExtra("snavid")) + 1800) * 1000;
        /*//w w  w  .j a  va2  s . c  om
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent timerExpire = new Intent(this, TimerExpiredReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, timerExpire, PendingIntent.FLAG_CANCEL_CURRENT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        am.setAlarmClock(new AlarmManager.AlarmClockInfo(wakeUpTime, sender), sender);
        } else {
        am.set(AlarmManager.RTC_WAKEUP, wakeUpTime, sender);
        }
        */
        Log.e("TAG", "4 bitince servisi durduracak, broadcast");

        Intent notificationIntent = new Intent(this, MainActivity.class);
        notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        notificationIntent.putExtra("yeni", "yeni");
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

        Log.e("TAG", "5 tklarsam ekran a");

        Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        Notification notification = new NotificationCompat.Builder(this).setContentTitle("Snav Devam Ediyor")
                .setTicker("Snav Devam Ediyor").setContentText("Snav Devam Ediyor")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(Bitmap.createScaledBitmap(icon, 128, 128, false)).setContentIntent(pendingIntent)
                .setOngoing(true).build();

        startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);
        Log.e("TAG", "6  Devam ediyor");

    } else if (intent.getAction().equals(Constants.ACTION.STOPFOREGROUND_ACTION)) {
        Log.i(LOG_TAG, "Received Stop Foreground Intent");

        Intent cancelIntent = new Intent(this, TimerExpiredReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(this, 0, cancelIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        am.cancel(sender);
        Log.e("TAG", "9  Receiver pta");

        stopForeground(true);
        stopSelf();

        Log.e("TAG", "10 Notif ptal");
    }
    return START_REDELIVER_INTENT;
}

From source file:com.github.sryze.wirebug.DebugStatusService.java

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent != null && intent.getAction() != null) {
        if (intent.getAction().equals(ACTION_UPDATE_STATUS)) {
            updateStatus();// w w w .  j a  v  a 2 s . c  om
            Intent updateStatusIntent = new Intent(DebugStatusService.this, DebugStatusService.class);
            updateStatusIntent.setAction(ACTION_UPDATE_STATUS);
            PendingIntent alarmPendingIntent = PendingIntent.getService(DebugStatusService.this, 0,
                    updateStatusIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            alarmManager.cancel(alarmPendingIntent);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME,
                    SystemClock.elapsedRealtime() + STATUS_UPDATE_INTERVAL, alarmPendingIntent);
        } else if (intent.getAction().equals(ACTION_STOP)) {
            DebugManager.setTcpDebuggingEnabled(false);
            updateStatus();
        }
    }
    return START_STICKY;
}

From source file:com.bayapps.android.robophish.MediaNotificationManager.java

public MediaNotificationManager(MusicService service) throws RemoteException {
    mService = service;/*from   w  ww. j  a  v a 2 s  . com*/
    updateSessionToken();

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

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

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

From source file:com.android.systemui.ReminderReceiver.java

@Override
public void onReceive(final Context context, Intent intent) {
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    SharedPreferences shared = context.getSharedPreferences(KEY_REMINDER_ACTION, Context.MODE_PRIVATE);

    if (intent.getBooleanExtra("clear", false)) {
        manager.cancel(NOTI_ID);//from w w w  .  ja va2s . c  o m
        // User has set a new reminder but didn't clear
        // This notification until now
        if (!shared.getBoolean("updated", false)) {
            shared.edit().putBoolean("scheduled", false).commit();
            shared.edit().putInt("hours", -1).commit();
            shared.edit().putInt("minutes", -1).commit();
            shared.edit().putInt("day", -1).commit();
            shared.edit().putString("title", null).commit();
            shared.edit().putString("message", null).commit();
        }
    } else {
        String title = shared.getString("title", null);
        String message = shared.getString("message", null);
        if (title != null && message != null) {
            Bitmap bm = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_qs_alarm_on);
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context).setTicker(title)
                    .setContentTitle(title).setContentText(message).setAutoCancel(false).setOngoing(true)
                    .setPriority(NotificationCompat.PRIORITY_HIGH).setSmallIcon(R.drawable.ic_qs_alarm_on)
                    .setLargeIcon(bm).setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(message));

            int alertMode = Settings.System.getIntForUser(context.getContentResolver(),
                    Settings.System.REMINDER_ALERT_NOTIFY, 0, UserHandle.USER_CURRENT);
            PendingIntent result = null;
            Intent serviceIntent = new Intent(context, ReminderService.class);
            if (alertMode != 0 && !QuietHoursHelper.inQuietHours(context, Settings.System.QUIET_HOURS_MUTE)) {
                context.startService(serviceIntent);
            }

            // Stop sound on click
            serviceIntent.putExtra("stopSelf", true);
            result = PendingIntent.getService(context, 1, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.setContentIntent(result);

            // Add button for dismissal
            serviceIntent.putExtra("dismissNoti", true);
            result = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.addAction(R.drawable.ic_sysbar_null,
                    context.getResources().getString(R.string.quick_settings_reminder_noti_dismiss), result);

            // Add button for reminding later
            serviceIntent.putExtra("time", true);
            result = PendingIntent.getService(context, 2, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
            builder.addAction(R.drawable.ic_qs_alarm_on,
                    context.getResources().getString(R.string.quick_settings_reminder_noti_later), result);

            shared.edit().putBoolean("scheduled", false).commit();
            shared.edit().putInt("hours", -1).commit();
            shared.edit().putInt("minutes", -1).commit();
            shared.edit().putInt("day", -1).commit();

            manager.notify(NOTI_ID, builder.build());
        }
    }
    shared.edit().putBoolean("updated", false).commit();
}

From source file:com.keylesspalace.tusky.util.NotificationManager.java

/**
 * Takes a given Mastodon notification and either creates a new Android notification or updates
 * the state of the existing notification to reflect the new interaction.
 *
 * @param context  to access application preferences and services
 * @param notifyId an arbitrary number to reference this notification for any future action
 * @param body     a new Mastodon notification
 *///w  w  w  .  j a v  a 2 s.co m
public static void make(final Context context, final int notifyId, Notification body) {
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    final SharedPreferences notificationPreferences = context.getSharedPreferences("Notifications",
            Context.MODE_PRIVATE);

    if (!filterNotification(preferences, body)) {
        return;
    }

    createNotificationChannels(context);

    String rawCurrentNotifications = notificationPreferences.getString("current", "[]");
    JSONArray currentNotifications;

    try {
        currentNotifications = new JSONArray(rawCurrentNotifications);
    } catch (JSONException e) {
        currentNotifications = new JSONArray();
    }

    boolean alreadyContains = false;

    for (int i = 0; i < currentNotifications.length(); i++) {
        try {
            if (currentNotifications.getString(i).equals(body.account.getDisplayName())) {
                alreadyContains = true;
            }
        } catch (JSONException e) {
            Log.d(TAG, Log.getStackTraceString(e));
        }
    }

    if (!alreadyContains) {
        currentNotifications.put(body.account.getDisplayName());
    }

    notificationPreferences.edit().putString("current", currentNotifications.toString()).apply();

    Intent resultIntent = new Intent(context, MainActivity.class);
    resultIntent.putExtra("tab_position", 1);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent deleteIntent = new Intent(context, NotificationClearBroadcastReceiver.class);
    PendingIntent deletePendingIntent = PendingIntent.getBroadcast(context, 0, deleteIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, getChannelId(body))
            .setSmallIcon(R.drawable.ic_notify).setContentIntent(resultPendingIntent)
            .setDeleteIntent(deletePendingIntent).setColor(ContextCompat.getColor(context, (R.color.primary)))
            .setDefaults(0); // So it doesn't ring twice, notify only in Target callback

    setupPreferences(preferences, builder);

    if (currentNotifications.length() == 1) {
        builder.setContentTitle(titleForType(context, body))
                .setContentText(truncateWithEllipses(bodyForType(body), 40));

        //load the avatar synchronously
        Bitmap accountAvatar;
        try {
            accountAvatar = Picasso.with(context).load(body.account.avatar)
                    .transform(new RoundedTransformation(7, 0)).get();
        } catch (IOException e) {
            Log.d(TAG, "error loading account avatar", e);
            accountAvatar = BitmapFactory.decodeResource(context.getResources(), R.drawable.avatar_default);
        }

        builder.setLargeIcon(accountAvatar);

    } else {
        try {
            String format = context.getString(R.string.notification_title_summary);
            String title = String.format(format, currentNotifications.length());
            String text = truncateWithEllipses(joinNames(context, currentNotifications), 40);
            builder.setContentTitle(title).setContentText(text);
        } catch (JSONException e) {
            Log.d(TAG, Log.getStackTraceString(e));
        }
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setVisibility(android.app.Notification.VISIBILITY_PRIVATE);
        builder.setCategory(android.app.Notification.CATEGORY_SOCIAL);
    }

    android.app.NotificationManager notificationManager = (android.app.NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    //noinspection ConstantConditions
    notificationManager.notify(notifyId, builder.build());
}

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

public int createCopyNotification(Context context, String programName) {
    if (context == null || programName == null) {
        return -1;
    }//www .  j  av a 2s  . c o  m
    initNotificationManager(context);

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

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

    NotificationData data = new NotificationData(context, pendingIntent,
            hku.fyp14017.blencode.R.drawable.ic_stat, programName,
            hku.fyp14017.blencode.R.string.notification_copy_title_pending,
            hku.fyp14017.blencode.R.string.notification_title_open,
            hku.fyp14017.blencode.R.string.notification_copy_pending,
            hku.fyp14017.blencode.R.string.notification_copy_finished);

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

From source file:net.frygo.findmybuddy.GCMIntentService.java

private static void generateNotification(Context context, String message) {

    Random rand = new Random();
    int x = rand.nextInt();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    String title = context.getString(R.string.app_name);
    Intent notificationIntent = new Intent(context, customlistview.class);
    notificationIntent.putExtra("alert", message);
    message = message + " would like to add you as friend";
    PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_CANCEL_CURRENT);

    Notification notification = new Notification(R.drawable.logo, message, System.currentTimeMillis());
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(x, notification);

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    final PowerManager.WakeLock mWakelock = pm
            .newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, title);
    mWakelock.acquire();// www  .  ja v a2s  .c  o m

    // Timer before putting Android Device to sleep mode.
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        public void run() {
            mWakelock.release();
        }
    };
    timer.schedule(task, 5000);

}

From source file:org.copticchurchlibrary.arabicreader.MediaNotificationManager.java

public MediaNotificationManager(MusicService service) throws RemoteException {
    mService = service;//w  w w  . j av a 2 s.com
    updateSessionToken();

    mNotificationColor = ResourceHelper.getThemeColor(mService,
            org.copticchurchlibrary.arabicreader.R.attr.colorPrimary, Color.DKGRAY);

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

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

From source file:com.zns.comicdroid.service.GoogleDriveService.java

private void NotifyAuthentication() {
    //Set prefs//from  www  .  j  a v  a  2s. c om
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    Editor editor = pref.edit();
    editor.putBoolean(Application.PREF_DRIVE_PUBLISH, false);
    editor.commit();

    Intent settingsIntent = new Intent(getApplicationContext(), Settings.class);
    settingsIntent.putExtra(Settings.INTENT_STOP_UPLOAD, true);

    TaskStackBuilder stack = TaskStackBuilder.create(getApplicationContext());
    stack.addParentStack(Settings.class);
    stack.addNextIntent(settingsIntent);

    PendingIntent pendingIntent = stack.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
            .setContentTitle(getString(R.string.nUploaderrorheading)).setSmallIcon(R.drawable.ic_launcher)
            .setContentText(getString(R.string.nUploaderrorsub)).setAutoCancel(true)
            .setContentIntent(pendingIntent);

    notificationManager.notify(1, builder.build());
}