我的 MainActicity
启动 RefreshService
与意图
这有一个布尔
额外名为 isNextWeek
。
我的 RefreshService
使通知
这将启动我的 MainActivity
这看起来是这样的:
Log.d(刷新,RefreshService有:isNextWeek:+将String.valueOf(isNextWeek));
意图notificationIntent =新的意图(这一点,MainActivity.class);
notificationIntent.putExtra(MainActivity.IS_NEXT_WEEK,isNextWeek);
Log.d(刷新,RefreshService放在意图:isNextWeek:+将String.valueOf(notificationIntent.getBooleanExtra(MainActivity.IS_NEXT_WEEK,FALSE)));
pendingIntent = PendingIntent.getActivity(此,0,notificationIntent,0);
建设者=新NotificationCompat.Builder(this).setContentTitle("Title").setContentText("ContentText").setSmallIcon(R.drawable.ic_notification).setContentIntent(pendingIntent);
通知= builder.build();
//隐藏其选中后通知
notification.flags | = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_REFRESH,通知);
正如你可以看到 notificationIntent
应具备的布尔
额外 IS_NEXT_WEEK
与 isNextWeek 的值被放在
PendingIntent
。
当我点击此通知
我总是假
作为价值 isNextWeek
这是我得到的价值的方式 MainActivity
:
isNextWeek = getIntent()getBooleanExtra(IS_NEXT_WEEK,假)。
日志:
08-04 00:19:32.500 13367-13367 / de.MayerhoferSimon.Vertretungsplan D /刷新:MainActivity发送:isNextWeek:真
08-04 00:19:32.510 13367-13573 / de.MayerhoferSimon.Vertretungsplan D /刷新:RefreshService有:isNextWeek:真
08-04 00:19:32.510 13367-13573 / de.MayerhoferSimon.Vertretungsplan D /刷新:RefreshService放在意图:isNextWeek:真
08-04 00:19:41.990 13367-13367 / de.MayerhoferSimon.Vertretungsplan D /刷新:MainActivity.onCreate有:isNextWeek:假的
当我直接启动 MainActivity
与意图
与ìsNextValue`是这样的:
意向书我=新的意图(这一点,MainActivity.class);
i.putExtra(IS_NEXT_WEEK,isNextWeek);
完();
startActivity(ⅰ);
一切工作正常,我也得到真
在 isNextWeek
是真
。
做什么我做了错误总是有一个假
值?
这解决了这个问题: http://stackoverflow.com/a/18049676/2180161
报价:的
我的怀疑是,因为唯一改变的意图是
临时演员,在 PendingIntent.getActivity(...)
工厂方法是
简单地重新使用旧的意图作为优化。
在RefreshService,请尝试:
PendingIntent pendingIntent = PendingIntent.getActivity(此,0,notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT);
请参阅:
http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT
解决方案我认为你需要更新意图
当你收到一个新的通过覆盖 onNewIntent(意向),在活动
。以下添加到您的活动:
@覆盖
公共无效onNewIntent(意向newIntent){
this.setIntent(newIntent);
//现在getIntent()返回更新意向
isNextWeek = getIntent()getBooleanExtra(IS_NEXT_WEEK,假)。
}
编辑:
这是只需要如果你的活动已经开始收到的意图时。如果你的活动开始(而不仅仅是恢复)的意图,那么问题是在其他地方,我的建议可能不会解决这个问题。
My MainActicity
starts RefreshService
with a Intent
which has a boolean
extra called isNextWeek
.
My RefreshService
makes a Notification
which starts my MainActivity
when the user clicks on it.
this looks like this:
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);
As you can see the notificationIntent
should have the boolean
extra IS_NEXT_WEEK
with the value of isNextWeek
which is put in the PendingIntent
.
When I click now this Notification
I always get false
as value of isNextWeek
This is the way I get the value in the MainActivity
:
isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);
Log:
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
When I directly start the MainActivity
with an Intent
with the ìsNextValue` like this:
Intent i = new Intent(this, MainActivity.class);
i.putExtra(IS_NEXT_WEEK, isNextWeek);
finish();
startActivity(i);
everything works fine and I get true
when isNextWeek
is true
.
What do I make wrong that there is always a false
value?
this solves the problem: http://stackoverflow.com/a/18049676/2180161
Quote:
My suspicion is that, since the only thing changing in the Intent is
the extras, the PendingIntent.getActivity(...)
factory method is
simply re-using the old intent as an optimization.
In RefreshService, try:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
See:
http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT
解决方案
I think you need to update the Intent
when you receive a new one by overriding onNewIntent(Intent)
in your Activity. Add the following to your Activity:
@Override
public void onNewIntent(Intent newIntent) {
this.setIntent(newIntent);
// Now getIntent() returns the updated Intent
isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);
}
Edit:
This is needed only if your Activity has already been started when the intent is received. If your activity is started (and not just resumed) by the intent, then the problem is elsewhere and my suggestion may not fix it.