我从aMainActicity
开始,其中有一个额外的.RefreshService
Intent
boolean
isNextWeek
当用户点击它时,MyRefreshService
会Notification
启动 my 。MainActivity
这看起来像这样:
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
应该有boolean
额外IS_NEXT_WEEK
的值,isNextWeek
其值放在PendingIntent
.
当我现在点击这个时Notification
,我总是false
得到isNextWeek
这是我获得价值的方式MainActivity
:
isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);
日志:
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
当我直接使用 ìsNextValue` 开始时MainActivity
,Intent
如下所示:
Intent i = new Intent(this, MainActivity.class);
i.putExtra(IS_NEXT_WEEK, isNextWeek);
finish();
startActivity(i);
一切正常,我什么true
时候isNextWeek
得到true
。
我做错了什么,总是有一个false
价值?
更新
这解决了问题: https ://stackoverflow.com/a/18049676/2180161
引用:
我的怀疑是,由于 Intent 中唯一改变的是额外内容,因此
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
请参阅下面的答案,为什么最好使用PendingIntent.FLAG_UPDATE_CURRENT
.