我想列出设备上安装的使用特定权限(如 INTERNET)的所有应用程序。

我正在使用此代码,但无法检索结果,请帮助。

private ArrayList<String> getInstalledApps(Context context) {
    ArrayList<String> results = new ArrayList<String>();
    PackageManager packageManager = context.getPackageManager();
    List<PackageInfo> applist = packageManager.getInstalledPackages(0);
    Iterator<PackageInfo> it = applist.iterator();
    while (it.hasNext()) {
        PackageInfo pk = (PackageInfo) it.next();
        if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(Manifest.permission.INTERNET, pk.packageName))
            results.add("" + pk.applicationInfo.loadLabel(packageManager));
    }

    for (int i = 0; i <= results.size(); i++) {
        Log.v("app using internet = ", results.toString());
    }

    return results;
}

最佳答案

我在 android 2.2 模拟器上尝试了代码,它运行良好。以下是工作代码:

private ArrayList<String> getInstalledApps(Context context) {
        ArrayList<String> results = new ArrayList<String>();
        PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> applist = packageManager.getInstalledPackages(0);
        Iterator<PackageInfo> it = applist.iterator();
        while (it.hasNext()) {
            PackageInfo pk = (PackageInfo) it.next();
            if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                Log.v("system app using internet = ", ""+pk.applicationInfo.loadLabel(packageManager));
                continue;
            }
            if (PackageManager.PERMISSION_GRANTED == packageManager
                    .checkPermission(Manifest.permission.INTERNET,
                            pk.packageName))
                results.add("" + pk.applicationInfo.loadLabel(packageManager));
        }

        Log.v("app using internet = ", results.toString());

        return results;
    }

关于android - 获取具有特定权限的应用程序名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13026257/

10-12 01:18