获取具有特定权限的应用程序名

时间:2012-10-23 08:25:32

标签: android android-manifest android-permissions

我想要设备上安装的所有应用程序的列表,这些应用程序使用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;
}

2 个答案:

答案 0 :(得分:3)

我尝试了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;
    }

答案 1 :(得分:1)

我有相同的情况,我使用以下检查解决了它

PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
if (PackageManager.PERMISSION_GRANTED != pm.checkPermission(Manifest.permission.INTERNET, p.packageName)) {
continue;
}

pack变量是一个类数组,用于保存有关已安装应用程序的信息,它包含的对象包含当前在系统中安装的所有应用程序的信息