首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用PendingIntent启动新活动时的混淆行为

使用PendingIntent启动新活动时的混淆行为
EN

Stack Overflow用户
提问于 2010-12-18 00:14:21
回答 3查看 3.9K关注 0票数 3

我正在使用BroadCast Listener监听来电统计信息,然后像这样推送通知:

代码语言:javascript
复制
    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);

因此,当有多个呼叫时,我希望有多个呼叫通知,当我单击每个通知时,我希望启动一个带有附加参数的活动。但是,我可以看到后面开始的活动得到了前一个调用的意图。

下面是我从活动中寻找意图的方法

代码语言:javascript
复制
        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));

我如何保持意图分离,调用独立的活动?

EN

回答 3

Stack Overflow用户

发布于 2011-02-16 15:36:27

试试这个:

代码语言:javascript
复制
PendingIntent.getActivity(context, 0 , notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

注意PendingIntent.getActitity方法的最后一个参数。如果您想知道应该使用哪个标志,请参阅API文档- [http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context](http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context),int,android.content.Intent,int)

1:http://developer.android.com/reference/android/app/PendingIntent.html#getActivity(android.content.Context

~多尔夫~

票数 3
EN

Stack Overflow用户

发布于 2013-03-01 18:56:14

它可以更容易地实现……只需覆盖activity类中的onNewIntent(Intent intent)方法,并在其中添加startactivity(intent)即可。

票数 2
EN

Stack Overflow用户

发布于 2010-12-22 15:00:35

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

以下是我的工作代码:

代码语言:javascript
复制
    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);

你看,我通过这样做在我的意图中设置了唯一的动作:

代码语言:javascript
复制
notificationIntent.setAction("com.vantage.vcrm.android.telephony"+System.currentTimeMillis());//unique per contact

另外,我在待定意向书中发送唯一的请求者ID:

代码语言:javascript
复制
PendingIntent.getActivity(context, (int) System.currentTimeMillis(), notificationIntent, 0);

这解决了问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4472447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档