Android 判断当前APP是否在前台

Android 判断当前APP是否在前台

isTopActivityAPP("com.test", 1)
    private boolean isTopActivityAPP(String packageName, int taskNumber) {
        boolean isTopActivity = false;
        ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
        String pkgName = activityManager.getRunningTasks(taskNumber).get(taskNumber - 1).topActivity.getPackageName();
        String topActivity = activityManager.getRunningTasks(taskNumber).get(taskNumber - 1).topActivity.getClassName();
        Log.e(TAG, "topActivity:" + topActivity);

        if (pkgName != null && pkgName.length() > 0 && pkgName.equals(packageName) && topActivity.equals(packageName + ".main.MainActivity")) {
            Log.e(TAG, "packageName: " + packageName + ",taskNumber:" + taskNumber);
            isTopActivity = true;
        } else {
            Log.e(TAG, "isTopActivityAPP: pkgName:" + pkgName);
        }
        return isTopActivity;
    }

你可能感兴趣的:(android,chrome,前端)