0

我正在尝试获取在我的应用程序中使用我的位置权限的应用程序列表,并且我正在尝试获取使用位置权限的应用程序:

    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.ACCESS_FINE_LOCATION") || permission.equals("android.permission.ACCESS_COARSE_LOCATION") || permission.equals("android.permission.ACCESS_MOCK_LOCATION") || permission.equals("android.permission.ACCESS_LOCATION_EXTRA_COMMANDS") || permission.equals("android.permission.ACCESS_INSTALL_LOCATION_PROVIDER")) {

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

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

                                    locationArray.add(appname);
                                }
                }
            }
        }
   } //end of getPermissions method

但是有了这个我会得到多个条目,比如

Angry Birds
Angry Birds
CWM
CWM
CWM
Facebook

有什么办法可以解决这个问题吗?

4

2 回答 2

1

获得重复条目是正常的,因为您遍历a 的所有权限PackageInfo并测试每个权限以查看它是否是与位置相关的权限之一。例如,当您迭代 Angry Birds 游戏的权限时,您会遇到两个权限(如果我没记错 ACCESS_FINE 和 ACCESS_COARSE),因为这些权限都满足 if 条件,所以您最终会添加 Angry Birds 两个次。

for (String permission : permissions)诀窍是在找到第一个权限时突破:

for (String permission : permissions) {
                    Log.d("TAG", permission);
                                if (permission.equals("android.permission.ACCESS_FINE_LOCATION") || permission.equals("android.permission.ACCESS_COARSE_LOCATION") || permission.equals("android.permission.ACCESS_MOCK_LOCATION") || permission.equals("android.permission.ACCESS_LOCATION_EXTRA_COMMANDS") || permission.equals("android.permission.ACCESS_INSTALL_LOCATION_PROVIDER")) {

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

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

                                    locationArray.add(appname);
                                    break;   
                                }
                }
于 2012-07-29T11:03:55.873 回答
1

如果您不想重复您可以将 locationArray 的所有内容添加到 HashSet(不允许重复)并将 HashSet 添加回 locationArray。

// add elements to hs, including duplicates
HashSet hs = new HashSet();
hs.addAll(localArray);
localArray.clear();
localArray.addAll(hs);

现在 localArray 不会有任何重复。

于 2012-07-29T11:08:53.887 回答