may*_*ysi 117 android android-intent android-service android-pendingintent
我MainActicity 开始RefreshService用Intent具有boolean所谓的多余isNextWeek.
我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);
Run Code Online (Sandbox Code Playgroud)
你可以看到notificationIntent应该有boolean额外IS_NEXT_WEEK的值,isNextWeek其中放入了PendingIntent.
当我现在点击这个时,Notification我总是得到false它的价值isNextWeek
这是我获得价值的方式MainActivity:
isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);
Run Code Online (Sandbox Code Playgroud)
日志:
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
Run Code Online (Sandbox Code Playgroud)
当我直接启动MainActivity带有Intent像这样的ìsNextValue:
Intent i = new Intent(this, MainActivity.class);
i.putExtra(IS_NEXT_WEEK, isNextWeek);
finish();
startActivity(i);
Run Code Online (Sandbox Code Playgroud)
一切正常,我什么true时候isNextWeek得到true.
我总是false有价值的错误是什么?
这解决了这个问题:https: //stackoverflow.com/a/18049676/2180161
引用:
我的怀疑是,由于Intent中唯一改变的是附加功能,因此
PendingIntent.getActivity(...)工厂方法只是将旧意图重新用作优化.在RefreshService中,尝试:
Run Code Online (Sandbox Code Playgroud)PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);看到:
http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT
请参阅下面的答案为什么最好使用PendingIntent.FLAG_UPDATE_CURRENT.
Yul*_*mok 22
使用PendingIntent.FLAG_CANCEL_CURRENT不是一个好的解决方案,因为内存使用效率低下.而是使用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
);
Run Code Online (Sandbox Code Playgroud)
然后:
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
int startPageNumber;
if ( savedInstanceState != null)
{
startPageNumber = savedInstanceState.getInt(PAGE_NUMBER_KEY);
//so on
Run Code Online (Sandbox Code Playgroud)
它现在应该工作.
如果你仍然没有预期的行为,尝试实现 void onNewIntent(Intent intent)事件处理程序,这样你就可以访问为活动调用的新意图(这与调用getIntent()不同,这将始终返回启动的第一个Intent你的活动.
@Override
protected void onNewIntent(Intent intent) {
int startPageNumber;
if (intent != null) {
startPageNumber = intent.getExtras().getInt(PAGE_NUMBER_KEY);
} else {
startPageNumber = 0;
}
}
Run Code Online (Sandbox Code Playgroud)
Vik*_*ram 19
我认为你需要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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
只有在收到意图时您的活动已经启动时才需要这样做.如果您的活动是由意图启动(而不仅仅是恢复),则问题出在其他地方,我的建议可能无法修复.
| 归档时间: |
|
| 查看次数: |
37813 次 |
| 最近记录: |