检查应用程序最小化/背景

编程入门 行业动态 更新时间:2024-10-25 08:20:08
本文介绍了检查应用程序最小化/背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很惊讶!

由于无法确定当前在前台或后台应用程序,

Because not able identify application currently in foreground or background,

当我pressed home键和我的应用程序是不是在前台。再后,我已经习惯了检查正在运行的应用程序在前台还是不行。

when i pressed home button and my application is not in foreground. then after i have used to check application running in foreground or not.

public boolean isAppOnForeground() { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { return false; } final String packageName = context.getPackageName(); for (RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { return true; } } return false; }

它会总是返回真正即使我的应用程序实际上不会在前台。

it will returns always true even if my application actually not in foreground.

注意: 我的应用程序有2个服务, 当应用程序从最近的任务列表中删除1服务将被取消(,我取得的成绩。) 当应用程序进入后台2服务将被取消。 (即检查WiFi信号强度。)

Note: My application have 2 services, 1 service will be canceled when application is removed from recent task list( that i have achieved.) 2 service will be canceled when application goes in background. (that is check wifi signal strength.)

如何知道应用程序是前景或背景?

如何在应用程序是不是在前台?停止服务

推荐答案

靠活动回调方法,如的onPause(),的onStop()和 onResume(),会给你提示的当前状态活动。该活动参考给每个这些方法的一个非常广泛的解释。

Rely on Activity callback methods, such as onPause(), onStop() and onResume(), that will give you hints on the current state of Activity. The Activity reference gives a very broad explanation of every of those methods.

更多推荐

检查应用程序最小化/背景

本文发布于:2023-11-04 05:20:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557053.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最小化   应用程序   背景

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!