首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PendingIntent不会发送额外的意图

PendingIntent不会发送额外的意图
EN

Stack Overflow用户
提问于 2013-08-04 06:38:26
回答 3查看 45.6K关注 0票数 138

我的MainActicity用一个Intent启动RefreshService,它有一个额外的名为isNextWeekboolean

我的RefreshService创建了一个Notification,当用户单击我的MainActivity时,它会启动它。

这看起来像这样:

代码语言:javascript
运行
复制
    Log.d("Refresh", "RefreshService got: isNextWeek: " + String.valueOf(isNextWeek));

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.putExtra(MainActivity.IS_NEXT_WEEK, isNextWeek);

    Log.d("Refresh", "RefreshService put in Intent: isNextWeek: " + String.valueOf(notificationIntent.getBooleanExtra(MainActivity.IS_NEXT_WEEK,false)));
    pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

    builder = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText("ContentText").setSmallIcon(R.drawable.ic_notification).setContentIntent(pendingIntent);
    notification = builder.build();
    // Hide the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(NOTIFICATION_REFRESH, notification);

正如您所看到的,notificationIntent应该具有booleanextra IS_NEXT_WEEK,其值为isNextWeek,它被放入PendingIntent中。

当我现在单击此Notification时,我总是得到false作为isNextWeek的值

这是我在MainActivity中获取值的方法

代码语言:javascript
运行
复制
    isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);

日志:

代码语言:javascript
运行
复制
08-04 00:19:32.500  13367-13367/de.MayerhoferSimon.Vertretungsplan D/Refresh: MainActivity sent: isNextWeek: true
08-04 00:19:32.510  13367-13573/de.MayerhoferSimon.Vertretungsplan D/Refresh: RefreshService got: isNextWeek: true
08-04 00:19:32.510  13367-13573/de.MayerhoferSimon.Vertretungsplan D/Refresh: RefreshService put in Intent: isNextWeek: true
08-04 00:19:41.990  13367-13367/de.MayerhoferSimon.Vertretungsplan D/Refresh: MainActivity.onCreate got: isNextWeek: false

当我直接用一个Intent启动MainActivity的时候,就像下面这样:

代码语言:javascript
运行
复制
    Intent i = new Intent(this, MainActivity.class);
    i.putExtra(IS_NEXT_WEEK, isNextWeek);
    finish();
    startActivity(i);

一切正常,当isNextWeektrue时,我得到了true

总是有一个false值,我做错了什么?

更新

这就解决了问题:https://stackoverflow.com/a/18049676/2180161

引用:

我怀疑,由于意图中唯一的变化是额外的,PendingIntent.getActivity(...)工厂方法只是简单地重用旧的意图作为优化。

在RefreshService中,尝试:

PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);

请参见:

http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT

更新2

请参阅answer below为什么使用PendingIntent.FLAG_UPDATE_CURRENT更好。

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

https://stackoverflow.com/questions/18037991

复制
相关文章

相似问题

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