判断当前应用程序处于前台还是后台

来源:互联网 发布:php做app 编辑:程序博客网 时间:2024/05/18 00:34
    /**     *判断当前应用程序处于前台还是后台,后台为true     */    public static boolean isApplicationBroughtToBackground(final Context context) {        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);        List<RunningTaskInfo> tasks = am.getRunningTasks(1);        if (!tasks.isEmpty()) {            ComponentName topActivity = tasks.get(0).topActivity;            if (!topActivity.getPackageName().equals(context.getPackageName())) {                return true;            }        }        return false;    }

1 1