Confused behavior when starting a new activity using PendingIntent

I use the BroadCast Listener to listen on an incoming phone, and then click on the notification as follows:

    CharSequence contentTitle = "Contact: " + number;
    CharSequence contentText = "Call From: " + number;
    Intent notificationIntent = new Intent(context, CallHandler.class);

    notificationIntent.putExtra(KEYS.NUMBER, number);
    notificationIntent.putExtra(KEYS.STATE, stat);
    notificationIntent.putExtra(KEYS.NAME, name);
    notificationIntent.putExtra(KEYS.DURATION, duration);
    notificationIntent.putExtra(KEYS.START_TIME, startTime);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    notif.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify((int) System.currentTimeMillis(), notif);

Therefore, when there are several calls, I expect that you will have several call notifications, and when I click each notification, I expect to start working with the attached parameters. However, I see that actions that start later get the intent of the previous call.

This is how I view intent from Activity

        Bundle extras = getIntent().getExtras();
    String number = extras.getString(KEYS.NUMBER);
    // String state = extras.getString(KEYS.STATE);
    long duration = extras.getLong(KEYS.DURATION);

    Date start = getStartDate();
    ((TextView) findViewById(R.id.subject)).setText("");
    ((TextView) findViewById(R.id.start_time)).setText(tf.format(start));
    ((TextView) findViewById(R.id.end_time)).setText(tf
            .format(getEndDate()));
    ((TextView) findViewById(R.id.caller_number)).setText(number);
    ((TextView) findViewById(R.id.notes)).setText(getNotesDefaultContent(
            number, duration, start));

How can I keep intentions separate regardless of activity?

+3
source share
3 answers

Try the following:

PendingIntent.getActivity(context, 0 , notificationIntent, PendintIntent.FLAG_UPDATE_CURRENT);

PendingIntent.getActitity. , FLAG , . API - [http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int)] [1]

[1]: http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context, int, android.content.Intent, int)

~ ~

+3

... onNewIntent(Intent intent)   startactivity(intent) .

+2

, , . , !

:

    CharSequence contentTitle = "Contact: " + number;
    CharSequence contentText = "Call From: " + number;
    Intent notificationIntent = new Intent(context, CallHandler.class);


    notificationIntent.setAction("com.vantage.vcrm.android.telephony"+System.currentTimeMillis());//unique per contact


    notificationIntent.putExtra(KEYS.NUMBER, number);
    notificationIntent.putExtra(KEYS.STATE, stat);
    notificationIntent.putExtra(KEYS.NAME, name);
    notificationIntent.putExtra(KEYS.DURATION, duration);
    notificationIntent.putExtra(KEYS.START_TIME, startTime);
    PendingIntent contentIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);

    notif.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

    notif.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify((int) System.currentTimeMillis(), notif);

, , :

notificationIntent.setAction("com.vantage.vcrm.android.telephony"+System.currentTimeMillis());//unique per contact

I also send a unique request identifier in pending intent:

PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);

This resolved the issue.

+1
source

Source: https://habr.com/ru/post/1780899/


All Articles