[英]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.