简体   繁体   English

如何查找应用程序是在后台运行还是被杀死的android?

[英]How to find whether app is running in background or killed android?

I'm having requirement where i need to check whether application is running in background or killed. 我在哪里需要检查应用程序是否在后台运行或已终止的要求。 I'm doing like as shown below. 我正在做如下图所示。

 ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

 List<ActivityManager.RunningTaskInfo> alltasks = am.getRunningTasks(1);
            for (ActivityManager.RunningTaskInfo aTask : alltasks) {
                // Used to check for CALL screen
                if (aTask.topActivity.getClassName().equals("com.android.phone.InCallScreen")
                        || aTask.topActivity.getClassName().equals("com.android.contacts.DialtactsActivity")) {
                    // When user on call screen show a alert message
                    Toast.makeText(context, "Phone Call Screen.", Toast.LENGTH_LONG).show();
                }

                // Used to check for SMS screen

                if (aTask.topActivity.getClassName().equals("com.android.mms.ui.ConversationList")
                        || aTask.topActivity.getClassName().equals("com.android.mms.ui.ComposeMessageActivity")) {
                    // When user on Send SMS screen show a alert message
                    Toast.makeText(context, "Send SMS Screen.", Toast.LENGTH_LONG).show();
                }


                // Used to check for CURRENT example main screen

                String packageName = "com.sap.rex.ui";

                if (aTask.topActivity.getPackageName().equals(packageName)) {
                    isInBackground = false;
                    Toast.makeText(context, "Current Example Screen.", Toast.LENGTH_LONG).show();
                }

The above code is only checking whether the top activity package name and telling whether it is in background or not. 上面的代码仅检查顶级活动包名称,并告诉其是否在后台。

If we open two or three application on top of it, since it only take top application package name it is saying it is not in background even though it is there. 如果我们在其顶部打开两个或三个应用程序,则由于它仅使用顶部应用程序包名称,因此即使它在后台也没有背景。

Please let me know where i'm doing wrong. 请让我知道我在哪里做错了。

Thanks 谢谢

I think you want to use getRunningAppProcesses() and check importance field against the flags defined in RunningAppProcessInfo . 我认为您想使用getRunningAppProcesses()并对照RunningAppProcessInfo中定义的标志检查重要性字段。 That should tell you if the app process is visible, background, or some other state. 那应该告诉您应用程序进程是可见的,背景还是其他状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何查找应用程序是在后台运行还是在前台运行或被杀死的android - How to find whether app is running in background or foreground or killed android Android如何在用户杀死应用程序后继续运行后台服务? - Android How to keep Running the background service when app is killed by user? “杀死”后在后台运行Android应用程序 - Running Android App in the Background after “killed” 我如何知道我的Android应用程序是否在后台运行? - How do I know whether or not my Android app is running in background? 我的应用程序在后台运行时被 android 系统杀死 - my app killed by android system when it running in background 如何让 flutter 应用程序在后台运行(防止被杀死) - How to Keep flutter app running in background (prevent from being killed) 进程在后台运行,计时器任务被Android杀死 - Process running in background with timer Task killed by android 在android中通过滑动杀死应用程序时如何处理正在运行的服务? - How to handle running service when app is killed by swiping in android? 在android中杀死应用程序后后台服务不起作用 - background service not working after app is killed in android Altbeacon Android-应用程序被杀死时EnterRegion(不是背景) - Altbeacon Android - EnterRegion while app is killed (not background)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM