3

我想要设备上安装的使用特定权限(如 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;
}
4

2 回答 2

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;
    }
于 2012-10-23T10:41:00.727 回答
1

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

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

packs 变量是一个类数组,其中包含有关已安装应用程序的信息,它由具有当前安装在系统中的所有应用程序信息的对象组成

于 2012-10-23T08:51:27.997 回答