如何在 android 中的棒棒糖版本之后获取包名称

How to get package name after lollipop version in android

我已经尝试使用下面的代码来获取前台应用程序包名称,但它只获取我自己的应用程序应用程序包名称。其他应用程序包名称将是 null.it 意味着它返回 null ...我怎样才能获得包名称..还有其他方法吗?我知道 getrunningappprocess 被 google.

阻止了
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    List<ActivityManager.RunningAppProcessInfo> tasks = manager.getRunningAppProcesses();
    String mpackageName = tasks.get(0).processName;
    UsageStatsManager usage = (UsageStatsManager) this.getSystemService(this.USAGE_STATS_SERVICE);
    long time = System.currentTimeMillis();

    List<UsageStats> appList = usage.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
    if (appList != null && appList.size() > 0) {
        SortedMap<Long, UsageStats> runningTask = new TreeMap<Long, UsageStats>();
        for (UsageStats usageStats : appList) {
            runningTask.put(usageStats.getLastTimeUsed(), usageStats);
        }
        if (!runningTask.isEmpty() && runningTask != null) {
            mpackageName = runningTask.get(runningTask.lastKey()).getPackageName();
        }
    }
    for (int i = 0; pakageName != null && i < pakageName.size(); i++) {
        Log.e("AppCheck", "comfor2 : " + mpackageName + "    pak2 : " + pakageName.get(i));
        if (mpackageName.equals(pakageName.get(i))) {
            currentApp = pakageName.get(i);
            return true;
        }
    }
}

通过以下方法获取 class 名称...

public static void classNameRunning(Context context) {
    boolean isInBackground = true;
    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
        List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
            if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                for (String activeProcess : processInfo.pkgList) {
                    if (activeProcess.equals(context.getPackageName())) {
                        // print log for class name
                    }
                }
            }
        }
    } else {
        List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
        ComponentName componentInfo = taskInfo.get(0).topActivity;
        if (componentInfo.getPackageName().equals(context.getPackageName())) {
            Log.w("Package name ==> ",componentInfo.getPackageName());
        }
    }


}
public static HashSet<String> getRunningApps(Context context) {
final HashSet<String> hashSet = new HashSet<String>();
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final PackageManager packageManager = context.getPackageManager();
List<RunningTaskInfo> runningTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo runningTaskInfo : runningTasks) {
    String packageName = runningTaskInfo.baseActivity.getPackageName();
    try {
        String appName = packageManager.getApplicationInfo(packageName, 0).loadLabel(packageManager).toString();
        hashSet.add(appName);
    } catch (NameNotFoundException exception) {
        // handle Exception
    }
}
return hashSet;
}