如何在android中的lollipop版本后获取包名称

时间:2017-03-07 10:47:08

标签: android android-service android-package-managers

我已经尝试过以下代码来获取forground应用程序包名称,但它只获取我自己的应用程序包名称。其他应用程序包的名称将为null。这意味着它返回null ...我怎样才能获得包名称..还有其他任何方式。我知道getrunningappprocess被谷歌阻止



(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;
                    }
                }
            }
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

从以下方法获取课程名称......

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());
        }
    }


}

答案 1 :(得分:1)

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;
}