应用程序被杀后,PendingIntent不会保留意图额外内容

时间:2015-02-27 14:57:59

标签: java android android-intent android-notifications android-pendingintent

我们遇到的问题是,如果应用程序因内存不足而被杀死,则不会存储来自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());

当应用程序尚未被操作系统杀死时,此功能正常...我们在onCreateonNewIntent方法中添加了一些代码来记录其他内容:

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上的文档特别说:

  

...即使其拥有的应用程序的进程被终止,PendingIntent本身也可以从已经给出它的其他进程中继续使用。

我们还尝试了不同的PendingIntent标志,但没有运气。

我们正在使用应用程序来填充手机的RAM以使手机处于低内存状态。

任何人都知道问题可能是什么?

1 个答案:

答案 0 :(得分:0)

我们最终发现了这一点 - 事实证明首先调用onCreate,然后调用onNewIntent

对我们来说掩盖问题的是,如果我们在onCreate中检测不到意图,那么我们会停止Activity并向用户显示其他内容。