Get application names with specific permission

2019-01-20 02:24发布

I want list of all applications installed on the device which use a specific permission like INTERNET.

i am using this code , but not able to retrieve results , please help .

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条回答
甜甜的少女心
2楼-- · 2019-01-20 03:14

i had the same scenario, i solved it using the following check

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

the packs variable is a array of class that holds information about the application installed and it consists of the objects with info of all the applications currently installed in system

查看更多
Rolldiameter
3楼-- · 2019-01-20 03:30

I tried the code on android 2.2 emulator and it was working fine. Following is the working code:

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;
    }
查看更多
登录 后发表回答