就简单的一个方法,就不用写什么步骤了:
public static boolean Frontdesk(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
Log.i(context.getPackageName(), "此appimportace ="
+ appProcess.importance
+ ",context.getClass().getName()="
+ context.getClass().getName());
if (appProcess.importance != ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
Log.i(context.getPackageName(), "处于后台"
+ appProcess.processName);
return true;
} else {
Log.i(context.getPackageName(), "处于前台"
+ appProcess.processName);
return false;
}
}
}
return false;
}
在需要的地方调用Frontdesk(context)方法即可
您可能感兴趣的文章:判断Android程序是否在前台运行的两种方法Android 判断某个服务(service)是否运行Android实现Service在前台运行服务Android 判断某个Activity 是否在前台运行的实例