首页 > 学院 > 开发设计 > 正文

判断app是否在前(后)台

2019-11-06 10:05:37
字体:
来源:转载
供稿:网友

测试数据及注意在最后

工具代码: 这2个方法几乎一样,区别在于判断参数:ActivityManager.RunningAppPRocessInfo.IMPORTANCE_FOREGROUND和ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND

public static boolean isBackground(Context context) { android.app.ActivityManager activityManager = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager .getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName())) { /* * BACKGROUND=400 EMPTY=500 FOREGROUND=100 GONE=1000 * PERCEPTIBLE=130 SERVICE=300 ISIBLE=200 */ Log.e(context.getPackageName(), "此appimportace =" + appProcess.importance + ",context.getClass().getName()=" + context.getClass().getName()); if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { Log.e(context.getPackageName(), "处于后台" + appProcess.processName); return true; } else { Log.e(context.getPackageName(), "处于前台" + appProcess.processName); return false; } } } return false; } public static boolean isBackground_2(Context context) { android.app. ActivityManager activityManager = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName())) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) { Log.e("后台", appProcess.processName); return true; }else{ Log.e("前台", appProcess.processName); return false; } } } return false; }

代码中使用:

public class Myapplication extends Application { private static MyApplication mContext = null; @Override public void onCreate() { super.onCreate(); mContext = this; } public static MyApplication getApplication() { return mContext; }} @Override protected void onResume() { super.onResume(); Log.e("MainActivity", "onResume"); Util.isBackground(MyApplication.getApplication()); Util.isBackground_2(MyApplication.getApplication()); } @Override protected void onPause() { super.onPause(); Log.e("MainActivity", "onPause"); Util.isBackground(MyApplication.getApplication()); Util.isBackground_2(MyApplication.getApplication()); } @Override protected void onStop() { super.onStop(); Log.e("MainActivity", "onStop"); Util.isBackground(MyApplication.getApplication()); Util.isBackground_2(MyApplication.getApplication()); }

测试数据:

打开02-27 10:00:29.818 17462-17462/com.chen.demo E/MainActivity: onResume02-27 10:00:29.821 17462-17462/com.chen.demo E/com.chen.demo: 此appimportace =100,context.getClass().getName()=com.chen.demo.MyApplication02-27 10:00:29.821 17462-17462/com.chen.demo E/com.chen.demo: 处于前台com.chen.demo02-27 10:00:29.823 17462-17462/com.chen.demo E/前台: com.chen.demo按下home键02-27 10:00:48.247 17462-17462/com.chen.demo E/MainActivity: onPause02-27 10:00:48.254 17462-17462/com.chen.demo E/com.chen.demo: 此appimportace =100,context.getClass().getName()=com.chen.demo.MyApplication02-27 10:00:48.254 17462-17462/com.chen.demo E/com.chen.demo: 处于前台com.chen.demo02-27 10:00:48.265 17462-17462/com.chen.demo E/前台: com.chen.demo02-27 10:00:48.513 17462-17462/com.chen.demo E/MainActivity: onStop02-27 10:00:48.515 17462-17462/com.chen.demo E/com.chen.demo: 此appimportace =400,context.getClass().getName()=com.chen.demo.MyApplication02-27 10:00:48.515 17462-17462/com.chen.demo E/com.chen.demo: 处于后台com.chen.demo02-27 10:00:48.516 17462-17462/com.chen.demo E/后台: com.chen.demo打开02-27 10:01:58.212 19567-19567/com.chen.demo E/MainActivity: onResume02-27 10:01:58.216 19567-19567/com.chen.demo E/com.chen.demo: 此appimportace =100,context.getClass().getName()=com.chen.demo.MyApplication02-27 10:01:58.216 19567-19567/com.chen.demo E/com.chen.demo: 处于前台com.chen.demo02-27 10:01:58.219 19567-19567/com.chen.demo E/前台: com.chen.demo按下锁屏键02-27 10:02:19.360 19567-19567/com.chen.demo E/MainActivity: onPause02-27 10:02:19.422 19567-19567/com.chen.demo E/com.chen.demo: 此appimportace =150,context.getClass().getName()=com.chen.demo.MyApplication02-27 10:02:19.422 19567-19567/com.chen.demo E/com.chen.demo: 处于后台com.chen.demo02-27 10:02:19.428 19567-19567/com.chen.demo E/前台: com.chen.demo02-27 10:02:19.430 19567-19567/com.chen.demo E/MainActivity: onStop02-27 10:02:19.437 19567-19567/com.chen.demo E/com.chen.demo: 此appimportace =150,context.getClass().getName()=com.chen.demo.MyApplication02-27 10:02:19.437 19567-19567/com.chen.demo E/com.chen.demo: 处于后台com.chen.demo02-27 10:02:19.439 19567-19567/com.chen.demo E/前台: com.chen.demo

注: 1、从上面的测试数据可以看出,在onPause()方法中,仍然不算做app进入后台,只有在onStop()才算 2、使用判断前后台参数不同,按下home键后判断结果一致;但是,按下锁屏键后,情况就不一样了,使用ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND参数的方法,将锁屏视为进入后台;但是,使用ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND参数的方法,仍将当前状态视为app在前台。在项目中使用的时候,可以根据这个特性区别对待


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表