1. 程式人生 > >判斷app是否在前臺執行

判斷app是否在前臺執行

     public static boolean isAppRunningForeground(String pkgname, Context context) {
        boolean isAppRunning = false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> list = am.getRunningTasks(1);
        for (RunningTaskInfo info : list) {
            if (info.topActivity.getPackageName().equals(pkgname) && info.baseActivity.getPackageName().equals(pkgname)) {
                isAppRunning = true;
                break;
            }
        }
        return isAppRunning;

    }