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.android.launcher3.Utilities.java

public static void restart(Context context, int delay) {
    if (delay == 0) {
        delay = 1;//  w  w  w  .  j av a 2s  .  c om
    }
    Intent restartIntent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
    restartIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    PendingIntent intent = PendingIntent.getActivity(context, 0, restartIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.RTC, java.lang.System.currentTimeMillis() + delay, intent);
    java.lang.System.exit(2);
}

From source file:jmri.enginedriver.threaded_application.java

/**
 * Display OnGoing Notification that indicates EngineDriver is Running.
 * Should only be called when ED is going into the background.
 * Currently call this from each activity onPause, passing the current intent 
 * to return to when reopening.  */
void addNotification(Intent notificationIntent) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.icon)
            .setContentTitle(this.getString(R.string.notification_title))
            .setContentText(this.getString(R.string.notification_text)).setOngoing(true);

    PendingIntent contentIntent = PendingIntent.getActivity(this, ED_NOTIFICATION_ID, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);
    builder.setContentIntent(contentIntent);

    // Add as notification
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(ED_NOTIFICATION_ID, builder.build());
}

From source file:com.sentaroh.android.TaskAutomation.TaskExecutor.java

final static private void showMessageNotification(TaskManagerParms taskMgrParms, EnvironmentParms envParms,
        CommonUtilities util, TaskResponse tr, String m_text, int led_color, int led_on, int led_off) {
    NotificationManager nm = (NotificationManager) taskMgrParms.context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    NotificationCompat.Builder nb = new NotificationCompat.Builder(taskMgrParms.context);
    nm.cancel("MSG", taskMgrParms.msgNotificationId);
    Intent in = new Intent();
    PendingIntent pi = PendingIntent.getActivity(taskMgrParms.context, 0, in,
            PendingIntent.FLAG_CANCEL_CURRENT);
    pi.cancel();//from   ww w.j  av  a  2 s. com
    nb.setContentIntent(pi).setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)
            .setOngoing(false).setAutoCancel(true).setSmallIcon(R.drawable.action)
            .setContentTitle(taskMgrParms.context.getString(R.string.app_name)).setContentText(m_text)
            .setWhen(System.currentTimeMillis());
    if (led_color != 0)
        nb.setLights(led_color, led_on, led_off);
    Notification nf = nb.build();
    nm.notify("MSG", taskMgrParms.msgNotificationId, nf);
    synchronized (envParms) {
        if (taskMgrParms.msgNotificationId >= MAX_NOTIFICATION_COUNT)
            taskMgrParms.msgNotificationId = 1;
        else
            taskMgrParms.msgNotificationId++;
    }
}

From source file:ru.orangesoftware.financisto2.export.flowzr.FlowzrSyncEngine.java

public static void builAndRun(Context context) {
    final FlowzrSyncActivity fa = FlowzrSyncActivity.getMySelf();
    if (fa == 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_sync_require_tap));
        nm.notify(SYNC_NOTIFICATION_ID, mNotifyBuilder.build());
        Log.e(TAG, "Sync unactive: the required activity is missing.");
        return;// www .  jav a  2s.  c o  m
    } else {
        if (FlowzrSyncActivity.isRunning) {
            Log.i(TAG, "Sync already in progress");
        } else {
            if ((System.currentTimeMillis() - FlowzrSyncOptions.last_sync_ts) > 30 * 1000) {
                Log.i(TAG, "Starting Auto-Sync Task");
                fa.runOnUiThread(new Runnable() {
                    public void run() {
                        fa.setRunning();
                    }
                });
                fa.initProgressDialog();
                new FlowzrSyncEngine(fa);
            } else {
                Log.i(TAG, "Sync have just been done");
            }
        }
    }
}

From source file:org.cryptsecure.Communicator.java

/**
 * Creates the notification. FromUid can either be a user uid in which case
 * it should be equal to item.from uid, or it could be a localgroupid in
 * which case it is equal to item.groupid.
 * //from  ww w  . j av  a2  s.c o  m
 * @param context
 *            the context
 * @param item
 *            the item
 * @param fromUid
 *            the from uid
 */
public static void createNotification(final Context context, ConversationItem item, int fromUid) {
    boolean vibrate = Utility.loadBooleanSetting(context, Setup.OPTION_VIBRATE, Setup.DEFAULT_VIBRATE);
    if (vibrate) {
        if (!Utility.isPhoneMuted(context)) {
            Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(200);
        }
    }

    boolean tone = Utility.loadBooleanSetting(context, Setup.OPTION_TONE, Setup.DEFAULT_TONE);
    if (tone) {
        if (!Utility.isPhoneMutedOrVibration(context)) {
            Utility.notfiyAlarm(context, RingtoneManager.TYPE_NOTIFICATION);
        }
    }

    // ALWAYS SET THE NOTIFICATION COUNTER!!! THEN ONLY RETURN FROM THIS
    // METHOD POSSIBLY IF THE
    // USER DOES NOT WANT TO SEE A REAL NOTIFICATION
    setNotificationCount(context, fromUid, false);

    boolean notification = Utility.loadBooleanSetting(context, Setup.OPTION_NOTIFICATION,
            Setup.DEFAULT_NOTIFICATION);
    if (!notification) {
        return;
    }

    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Intent notificationIntent = new Intent(context, TransitActivity.class);
    notificationIntent = notificationIntent.putExtra(Setup.INTENTEXTRA, fromUid);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent,
            PendingIntent.FLAG_CANCEL_CURRENT);

    String completeMessage = item.text;
    if (completeMessage == null) {
        completeMessage = "";
    }
    String completeTextWithoutImages = Conversation.possiblyRemoveImageAttachments(context, completeMessage,
            true, "[ image ]", -1);

    int cnt = getNotificationCount(context, fromUid);
    String title = Main.UID2Name(context, fromUid, false);
    String text = completeTextWithoutImages;
    if (cnt > 1) {
        text = cnt + " new messages";
    }

    int maxWidth = Utility.getScreenWidth(context) - 80;
    text = Utility.cutTextIntoOneLine(text, maxWidth, 25);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.msgsmall24x24).setPriority(NotificationCompat.PRIORITY_MAX)
            .setCategory(NotificationCompat.CATEGORY_ALARM).setTicker(title + ": " + completeTextWithoutImages)
            .setWhen(0).setContentTitle(title).setContentText(text).setContentIntent(pendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

    mBuilder.setGroup(Setup.GROUP_CRYPTSECURE);
    mBuilder.setAutoCancel(true);
    Notification n = mBuilder.build();

    n.contentIntent = pendingIntent;
    int notificationId = 8888888 + fromUid;
    notificationManager.notify(notificationId, n);
}

From source file:org.restcomm.android.sdk.RCDevice.java

private void notificationHandleForegroundUpdate(RCConnection connection) {
    String peerUsername = connection.getPeer().replaceAll(".*?sip:", "").replaceAll("@.*$", "");

    callIntent.setAction(ACTION_RESUME_CALL);
    callIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    // Intent to open the call activity (for when tapping on the general notification area)
    Intent serviceIntentMute = new Intent(ACTION_NOTIFICATION_CALL_MUTE_AUDIO, null, getApplicationContext(),
            RCDevice.class);
    // Intent to decline the call without opening the App Activity
    Intent serviceIntentDisconnect = new Intent(ACTION_NOTIFICATION_CALL_DISCONNECT, null,
            getApplicationContext(), RCDevice.class);

    int resId = R.drawable.ic_mic_24dp;
    String muteString = "Unmuted";
    if (connection.isAudioMuted()) {
        resId = R.drawable.ic_mic_off_24dp;
        muteString = "Muted";
    }/*w ww .  ja v  a 2  s .  c o m*/

    // we dont need high importance, user knows he is on the call
    NotificationCompat.Builder builder = getNotificationBuilder(false);

    builder.setSmallIcon(R.drawable.ic_phone_in_talk_24dp).setContentTitle(peerUsername)
            .setContentText("Tap to return to call")
            // Notice that for some reason using FLAG_UPDATE_CURRENT doesn't work. The problem is that the intent creates a new Call Activity instead of
            // taking us to the existing.
            .addAction(resId, muteString,
                    PendingIntent.getService(getApplicationContext(), 0, serviceIntentMute,
                            PendingIntent.FLAG_CANCEL_CURRENT))
            .addAction(R.drawable.ic_call_end_24dp, "Hang up",
                    PendingIntent.getService(getApplicationContext(), 0, serviceIntentDisconnect,
                            PendingIntent.FLAG_CANCEL_CURRENT))
            .setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, callIntent,
                    PendingIntent.FLAG_CANCEL_CURRENT));

    startForeground(ONCALL_NOTIFICATION_ID, builder.build());
}

From source file:de.anderdonau.spacetrader.Main.java

public void btnChangeTheme(View view) {
    SharedPreferences sp = getSharedPreferences("spacetrader", MODE_PRIVATE);
    SharedPreferences.Editor ed = sp.edit();
    String theme = sp.getString("Theme", "Light");
    if (view.getId() == R.id.btnDarkTheme) {
        if ("Dark".equals(theme)) {
            Toast.makeText(this, "This theme is already selected.", Toast.LENGTH_SHORT).show();
            return;
        }//from   w ww . j av a 2s. c  o  m
        ed.putString("Theme", "Dark");
    } else {
        if ("Light".equals(theme)) {
            Toast.makeText(this, "This theme is already selected.", Toast.LENGTH_SHORT).show();
            return;
        }
        ed.putString("Theme", "Light");
    }
    ed.commit();
    Popup popup = new Popup(this, "Change Theme",
            "Space Trader must be restarted to change the theme. Do you want to do that now?", "",
            "Restart now", "Restart later", new Popup.buttonCallback() {
                @Override
                public void execute(Popup popup, View view) {
                    saveGame();
                    Intent mStartActivity = new Intent(getApplicationContext(), Main.class);
                    int mPendingIntentId = Math.abs(gameState.rand.nextInt());
                    //noinspection ConstantConditions
                    PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(),
                            mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
                    AlarmManager mgr = (AlarmManager) getApplicationContext()
                            .getSystemService(Context.ALARM_SERVICE);
                    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
                    System.exit(0);
                }
            }, cbShowNextPopup);
    addPopup(popup);
    showNextPopup();
}

From source file:android.app.Activity.java

/**
 * @hide//  w w  w.j  a va  2s  .com
 */
private void showMigrateNotification() {

    boolean flag = false;

    if (mMigrator == null) {
        mMigrator = IMigratorService.Stub.asInterface(ServiceManager.getService("Migrator"));
    }
    try {
        /* judge whether this App can migrate */
        flag = mMigrator.checkMigratableApp();
    } catch (RemoteException e) {
        Log.d(TAG, "Migrate failed in Dialog");
    }

    if (flag) {
        Intent intent = new Intent();
        intent.setClassName("com.android.migrationmanager", "com.android.migrationmanager.DeviceListDialog");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        /* on tapping, show dialog Activity */

        Notification notification = new Notification.Builder(this)
                .setContentTitle("Migrator in " + getAppName())
                .setContentText("start Migration: " + getLocalClassName())
                .setSmallIcon(com.android.internal.R.drawable.ic_menu_send).setAutoCancel(true)
                .setContentIntent(pi).build();

        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        nm.notify(Process.myUid(), notification);
    }
}