android中其他应用使用的权限

时间:2014-03-28 05:18:02

标签: permissions

我想在我的Android应用中添加一项功能,可以检查其他已安装应用使用的权限。例如: - 我的应用程序检查其他已安装的应用程序权限,使用它们?

 StringBuffer appNameAndPermissions=new StringBuffer();
PackageManager pm = getPackageManager();
List <ApplicationInfo>packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
{
for (ApplicationInfo applicationInfo : packages) {
Log.d("test", "App: " + applicationInfo.name + " Package: " + applicationInfo.packageName);
try {
PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
              appNameAndPermissions.append(packageInfo.packageName+"*:\n"); 
    //  Get Permissions
String[] requestedPermissions = packageInfo.requestedPermissions;
 if(requestedPermissions != null) {
 for (int i = 0; i < requestedPermissions.length; i++) {
 Log.d("test", requestedPermissions[i]);
appNameAndPermissions.append(requestedPermissions[i]+"\n");
}
 appNameAndPermissions.append("\n");
}
} catch (NameNotFoundException e) {
 e.printStackTrace();
}}} 

}

这是有效的代码,它提供了所有应用使用的所有权限的完整列表。 但我的问题是如何知道哪个应用程序使用哪个权限?

请帮助我&amp;让我对此有所了解。

1 个答案:

答案 0 :(得分:0)

这是工作代码:

PackageManager mPm = getPackageManager();   
List <PackageInfo> appList=mPm.getInstalledPackages(PackageManager.GET_PERMISSIONS|PackageManager.GET_RECEIVERS|
          PackageManager.GET_SERVICES|PackageManager.GET_PROVIDERS);

for (PackageInfo pi : appList) {
    System.out.println("Process Name: "+pi);
    // Do not add System Packages
    if ((pi.requestedPermissions == null || pi.packageName.equals("android")) || 
            (pi.applicationInfo != null && (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)) 
        continue;

    for (String permission : pi.requestedPermissions) {
        //Map<String, String> curChildMap = new HashMap<String, String>();
        //System.out.println("############     "+permission);

        try {
            PermissionInfo pinfo = mPm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
            CharSequence label = pinfo.loadLabel(mPm);
            CharSequence desc = pinfo.loadDescription(mPm);
            System.out.println("$$$$$ "+label+"!!!!!! "+desc);

        } catch (NameNotFoundException e) {
            Log.i(TAG, "Ignoring unknown permission " + permission);
            continue;
       }
    }