PendingIntent 在应用程序被杀死后不会保留 Intent Extras
PendingIntent Doesn't Keep Intent Extras After App is Killed
我们遇到一个问题,如果应用程序因内存不足而被终止,则不会存储来自 PendingIntent 的 Intent 的额外捆绑包。
我们创建通知:
// ...
final Intent intent = new Intent(this, ControllerActivity.class);
intent.putExtra(KEY_PUSH_NOTIFICATION, notification);
PendingIntent pIntent = PendingIntent.getActivity(this, 9349302, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pIntent);
//...
notificationManager.notify(123, builder.build());
当应用程序没有被 OS 杀死时,这工作正常...我们在 onCreate
和 onNewIntent
方法中添加了一些代码来记录额外内容:
Log.i("Test", "Intent Extras...");
if (intent.getExtras() != null) {
for (String key : intent.getExtras().keySet()) {
Log.i("Test", "Intent Extra: " + intent.getExtras().get(key));
}
}
这是 LogCat 当应用程序尚未被杀死并且我们打开通知时:
I/Test﹕ Intent: Intent { Htcflg=0x1 cmp=com.package.removed.debug/com.package.removed.ControllerActivity bnds=[0,379][1080,571] (has extras) }
I/Test﹕ Intent Extra: com.package.removed.push.PushNotification@425f74b8
这是应用 被终止时的 LogCat:
I/Test﹕ Intent: Intent { cmp=com.package.removed.debug/com.package.removed.ControllerActivity }
这看起来很奇怪,因为 PendingIntent
上的文档特别指出:
...even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.
我们还尝试了不同的 PendingIntent 标志,但没有成功。
我们正在使用一个应用程序来填充 phone 的 RAM 以使 phone 进入低内存状态。
有人知道问题出在哪里吗?
我们最终弄明白了这一点 - 结果是先调用了 onCreate
,然后调用了 onNewIntent
。
对我们来说掩盖问题的是,如果我们在 onCreate
中未检测到任何意图,那么我们将停止 Activity
并向用户显示其他内容。
我们遇到一个问题,如果应用程序因内存不足而被终止,则不会存储来自 PendingIntent 的 Intent 的额外捆绑包。
我们创建通知:
// ...
final Intent intent = new Intent(this, ControllerActivity.class);
intent.putExtra(KEY_PUSH_NOTIFICATION, notification);
PendingIntent pIntent = PendingIntent.getActivity(this, 9349302, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pIntent);
//...
notificationManager.notify(123, builder.build());
当应用程序没有被 OS 杀死时,这工作正常...我们在 onCreate
和 onNewIntent
方法中添加了一些代码来记录额外内容:
Log.i("Test", "Intent Extras...");
if (intent.getExtras() != null) {
for (String key : intent.getExtras().keySet()) {
Log.i("Test", "Intent Extra: " + intent.getExtras().get(key));
}
}
这是 LogCat 当应用程序尚未被杀死并且我们打开通知时:
I/Test﹕ Intent: Intent { Htcflg=0x1 cmp=com.package.removed.debug/com.package.removed.ControllerActivity bnds=[0,379][1080,571] (has extras) }
I/Test﹕ Intent Extra: com.package.removed.push.PushNotification@425f74b8
这是应用 被终止时的 LogCat:
I/Test﹕ Intent: Intent { cmp=com.package.removed.debug/com.package.removed.ControllerActivity }
这看起来很奇怪,因为 PendingIntent
上的文档特别指出:
...even if its owning application's process is killed, the PendingIntent itself will remain usable from other processes that have been given it.
我们还尝试了不同的 PendingIntent 标志,但没有成功。
我们正在使用一个应用程序来填充 phone 的 RAM 以使 phone 进入低内存状态。
有人知道问题出在哪里吗?
我们最终弄明白了这一点 - 结果是先调用了 onCreate
,然后调用了 onNewIntent
。
对我们来说掩盖问题的是,如果我们在 onCreate
中未检测到任何意图,那么我们将停止 Activity
并向用户显示其他内容。