PendingIntent不发送意图额外

我的MainActicity启动了一个带有一个名为isNextWeekboolean额外的Intent RefreshService

我的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应该有一个额外的IS_NEXT_WEEK ,它的值是放在PendingIntentisNextWeek

当我点击这个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 

当我用像这样的“NextValue”的Intent直接启动MainActivity时:

  Intent i = new Intent(this, MainActivity.class); i.putExtra(IS_NEXT_WEEK, isNextWeek); finish(); startActivity(i); 

一切工作正常,当isNextWeektrue时,我变得true

我有什么错误的地方总是有false价值?

UPDATE

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

引用:

我的怀疑是,因为Intent中唯一改变的是extras,所以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

我想你需要更新的Intent当你收到一个新的重写onNewIntent(Intent)在你的活动。 将以下内容添加到您的活动中:

 @Override public void onNewIntent(Intent newIntent) { this.setIntent(newIntent); // Now getIntent() returns the updated Intent isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false); } 

编辑:

只有在收到意图时您的活动已经启动,这是必需的。 如果你的活动开始(而不是只是恢复)的意图,那么问题在别处,我的build议可能无法解决它。

使用PendingIntent.FLAG_CANCEL_CURRENT不是一个好的解决scheme,因为内存使用效率低下。 而是使用PendingIntent.FLAG_UPDATE_CURRENT

也使用Intent.FLAG_ACTIVITY_SINGLE_TOP (如果活动已经在历史堆栈顶部运行,则不会启动该活动)。

 Intent resultIntent = new Intent(this, FragmentPagerSupportActivity.class). addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); resultIntent.putExtra(FragmentPagerSupportActivity.PAGE_NUMBER_KEY, pageNumber); PendingIntent resultPendingIntent = PendingIntent.getActivity( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT ); 

然后:

 @Override protected void onCreate(Bundle savedInstanceState) { try { super.onCreate(savedInstanceState); int startPageNumber; if ( savedInstanceState != null) { startPageNumber = savedInstanceState.getInt(PAGE_NUMBER_KEY); //so on 

它应该现在工作。


如果你还没有预料到行为,尝试实现void onNewIntent(Intent intent)事件处理程序,这样你就可以访问被调用的新的intent(不同于调用getIntent(),这将永远返回启动您的活动的第一个意图。

 @Override protected void onNewIntent(Intent intent) { int startPageNumber; if (intent != null) { startPageNumber = intent.getExtras().getInt(PAGE_NUMBER_KEY); } else { startPageNumber = 0; } } 

以下代码应该可以工作: –

 int icon = R.drawable.icon; String message = "hello"; long when = System.currentTimeMillis(); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(icon, message, when); Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.putExtra("isNexWeek", true); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); notification.setLatestEventInfo(context, title, message, pIntent); notification.flags |= Notification.FLAG_AUTO_CANCEL; notificationManager.notify(0, notification); 

在MainActivity onCreate中:

 if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("isNextWeek")) { boolean isNextWeek = getIntent().getExtras().getBoolean("isNextWeek"); }