使用PendingIntent启动新活动时出现混乱的行为

3

我正在使用广播监听器来监听手机状态,然后推送通知,代码如下:

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

所以当有多个调用时,我期望有多个调用通知,并且当我点击每个通知时,我期望启动一个带有附加参数的活动。但是,我发现后面启动的活动会获得先前调用的 Intent。
以下是我从活动中查看意图的方式:
        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));

如何保持意图分开调用独立的活动?

我在这里遇到了同样的问题:https://dev59.com/wm855IYBdhLWcg3wcz1h,但仍然无法解决。 - ninjasense
3个回答

3

试试这个:

PendingIntent.getActivity(context, 0 , notificationIntent, PendingIntent.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)
~Dolph~

它是 PendingIntent 而不是 PendintIntent。 - Maveňツ

2

甚至更容易实现...只需在activity类中覆盖onNewIntent(Intent intent)方法,然后在其中添加startActivity(intent)即可。


1

好的,我想我已经找到了一个解决方案。诀窍在于正确创建Intent!

这是我的可行代码:

    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

同时,我在待定意图中发送唯一的请求者ID:

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

这解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接