android 判断app是否处于前台
逻辑是获取系统的运行栈,处于栈顶的就是处于前台的app,然后对比包名,不同则表示app处于后台,反之,则表示app处于前台.
/**
* app是否在后台
*
* @param context
* @return true 是 false 不是
*/
public static boolean isBackground(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
StaticMethod.debugEMSG(topActivity.getPackageName() + " : " + context.getPackageName());
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
浙公网安备 33010602011771号