迭代List以查找权限

时间:2012-07-23 22:59:03

标签: android list permissions iteration loops

我目前正在尝试遍历列表以从手机上的所有已安装应用程序中找到某个权限,有人可以告诉我迭代列表的正确方法吗?

public void getPermissions(Context context) {
    PackageManager packageManager = context.getPackageManager();
    final List<PackageInfo> apps = context.getPackageManager()
        .getInstalledPackages(PackageManager.GET_PERMISSIONS);

    for (PackageInfo pi : apps) {
        String[] permissions = pi.requestedPermissions;
        if (permissions != null) {
            for (String permission : permissions) {
                Log.d("TAG", permission);
                            if (permission.equals("android.permission.INTERNET")) {

                                String appname=pi.applicationInfo.loadLabel(packageManager).toString();
                                ImageView appicon;

                                appicon = pi.applicationInfo.setImageDrawable(packageManager);
                                Log.e("TAG", "Permission found for "+ appname);

                                internetArray.add(appname);
                            }
            }
        }
    }

} // getPermissions方法结束

编辑:我已经设法开始工作并在arraylist中显示。

1 个答案:

答案 0 :(得分:2)

public void getPermissions(Context context) {
     PackageManager packageManager = context.getPackageManager();
     final List<PackageInfo> apps = context.getPackageManager()
         .getInstalledPackages(PackageManager.GET_PERMISSIONS);

     for (PackageInfo pi : apps) {
         String[] permissions = pi.requestedPermissions;
         if (permissions != null) {
             for (String permission : permissions) {
                 Log.d("TAG", permission);
                 if (permission.equals("android.permission.INTERNET")) {
                     // do something
                 }
             }
         }
     }
}