Android 判断app是否在前台还是在后台运行
作者:互联网
Android 判断app是否在前台还是在后台运行,直接看代码,可直接使用。
[java] view plaincopy
- public static boolean isBackground(Context context) {
- ActivityManager activityManager = (ActivityManager) context
- .getSystemService(Context.ACTIVITY_SERVICE);
- List<RunningAppProcessInfo> appProcesses = activityManager
- .getRunningAppProcesses();
- for (RunningAppProcessInfo appProcess : appProcesses) {
- if (appProcess.processName.equals(context.getPackageName())) {
- /*
- BACKGROUND=400 EMPTY=500 FOREGROUND=100
- GONE=1000 PERCEPTIBLE=130 SERVICE=300 ISIBLE=200
- */
- Log.i(context.getPackageName(), "此appimportace ="
- + appProcess.importance
- + ",context.getClass().getName()="
- + context.getClass().getName());
- if (appProcess.importance != RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
- Log.i(context.getPackageName(), "处于后台"
- + appProcess.processName);
- return true;
- } else {
- Log.i(context.getPackageName(), "处于前台"
- + appProcess.processName);
- return false;
- }
- }
- }
- return false;
- }
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!http://www.captainbed.net
标签:return,Log,app,getPackageName,processName,前台,context,appProcess,Android 来源: https://www.cnblogs.com/skiwnchhw/p/10472396.html