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

时间:2010-12-17 16:14:21

标签: android

我正在使用BroadCast Listener收听传入的手机状态,然后按下这样的通知:

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

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

3 个答案:

答案 0 :(得分: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)

〜多尔夫伦德〜

答案 1 :(得分:2)

可以更轻松地实现...只需在活动类中覆盖onNewIntent(Intent intent)方法,只需在其中添加startactivity(intent)

答案 2 :(得分:1)

好的,我想我找到了解决方案。诀窍是正确创建意图!

这是我的工作代码:

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

这解决了这个问题。