Android 判断程序在前台运行还是后台运行
就简单的一个方法,就不用写什么步骤了:
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 判断程序在前台运行还是后台运行 的全部内容, 来源链接: utcz.com/z/342626.html