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

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

问题描述

Android 操作系统在内存不足时会终止进程.场景: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 通过通知重新打开它,我不知道如何确定应用进程是否被 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 在从通知中打开新 Activity 时是否杀死了应用程序进程?

我找到了一个解决这个问题的方法.使用:Android:点击通知时总是启动最重要的活动 如果 Android 终止应用程序进程并处理重新初始化,我可以打开堆栈顶部的活动,该活动将传递一个 savedInstanceState.然后每个活动负责使用原始通知意图中的 Extras 将用户重定向到适当的活动.此场景的意图设置如下:

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 的幼稚理解似乎让我失望,所以它可能没有意义):是否有我可以设置的操作、类别或标志在 Intent 上,就像在上面的片段中一样,这将允许我确定应用进程是否已被操作系统终止?

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 是否已终止/重新创建任务,如果应用程序尚未初始化,您可以重定向到根 Activity 或调用初始化方法或其他任何...像这样:

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
}

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

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