是否可以从Notification Intent/PendingIntent中检测Android是否杀死了应用程序(任务进程)? [英] Can I detect if Android has killed the application (task process) from a Notification Intent / PendingIntent?

本文介绍了是否可以从Notification Intent/PendingIntent中检测Android是否杀死了应用程序(任务进程)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android OS内存不足时会终止进程.场景:Android终止了应用程序进程,我通过Android启动器或近期任务列表(长按主屏幕按钮)重新打开了该进程.我可以使用以下方法检查Android是否在最近查看的活动的onCreate()方法中杀死了我的应用程序进程:

The Android OS kills processes when it's low on memory. Scenario: Android kills the app process and I re-open it through either the Android launcher or the recent-task list (long press home button). I can check if Android killed my app process in the onCreate() method of the most recently viewed activity using:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    if (savedInstanceState != null) {
        // Re-initialise things that killing the app would have destroyed
    }
}

但是,如果Android终止了应用程序进程,而我却使用PendingIntent内的Intent通过Notification重新打开了该进程,则我不知道如何确定该应用程序进程是否已被Android终止.随后,我不会重新初始化杀死应用程序进程会破坏的事情.

However, if Android kills the app process and I re-open it through a Notification using an Intent packaged inside a PendingIntent, I don't know how to determine if the app process was killed by Android. Subsequently I do not re-initialise things that killing the app process would have destroyed.

是否有办法确定从通知中打开新活动时Android是否终止了应用程序进程?

我已经找到了解决该问题的一个好办法.使用: Android:点击通知时始终启动热门活动如果Android终止了应用程序进程并进行重新初始化,我可以在传递了saveInstanceState的堆栈上打开活动.然后,每个活动都负责使用原始通知意图"中的附加功能"将用户重定向到适当的活动.这种情况的意图设置如下:

I have found one hacky solution to this problem. Using: Android: always launch top activity when clicked on notification I can open the activity on top of the stack which is passed a savedInstanceState if Android killed the app process and deal with re-initialisation. Each activity is then responsible for redirecting the user to the appropriate activity using Extras in the original Notification Intent. Intent setup for this scenario is below:

Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

我可以在Intent上设置一个操作,类别或标志,以模拟重新打开应用程序的过程,就像用户完成操作一样,但是在新的Intent/Activity上设置

编辑:为了澄清最后一个问题(尽管我对Android的婴儿般的理解使我失望,所以这可能没有任何意义):是否可以设置Action,Category或Flag像上面的代码片段中所述,可以让我确定应用程序进程是否已被操作系统杀死?

To clarify the last question (although it seems my infant understanding of Android is failing me so it probably doesn't make sense): Is there an Action, Category or Flag I can set on an Intent, like in the snippet above, that will allow me to determine if the app process has been killed by the OS?

推荐答案

确定Android是否已杀死该进程然后创建新进程的最简单方法如下:

The easiest way to determine if Android has killed the process and then created a new process is as follows:

在您的根活动(具有ACTION = MAIN和CATEGORY = DEFAULT的活动)中,创建一个公共静态布尔变量,如下所示:

In your root Activity (the one with ACTION=MAIN and CATEGORY=DEFAULT) create a public static boolean variable like this:

public static boolean initialized;

在根活动的onCreate()中,将此变量设置为true.

in onCreate() of your root Activity, set this variable to true.

在所有其他活动的onCreate()中,您可以通过检查布尔值的状态来检查Android是否已杀死/重新创建了任务,如果尚未初始化应用程序,则可以重定向到根活动或调用初始化方法之类的...

In onCreate() of all your other activities, you can check if Android has killed/recreated the task by checking the state of the boolean, and if the app hasn't been initialized, you can redirect to the root Activity or call an initialization method or whatever... like this:

if (!RootActivity.initialized) {
    // Android has killed and recreated the process and launched this
    //  Activity. We need to reinitialize everything now
    ... redirect to root activity or call reinitialize method
}

这篇关于是否可以从Notification Intent/PendingIntent中检测Android是否杀死了应用程序(任务进程)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆