我的MainActicity
用一个Intent
启动RefreshService
,它有一个额外的名为isNextWeek
的boolean
。
我的RefreshService
创建了一个Notification
,当用户单击我的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
extra 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
当我直接用一个Intent
启动MainActivity
的时候,就像下面这样:
Intent i = new Intent(this, MainActivity.class);
i.putExtra(IS_NEXT_WEEK, isNextWeek);
finish();
startActivity(i);
一切正常,当isNextWeek
为true
时,我得到了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
更好。
https://stackoverflow.com/questions/18037991
复制相似问题