我必须确定我的设备上已安装的应用程序使用的权限列表。
我已经安装了应用程序列表,并使用以下代码获得了包名称:
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> list = m.queryIntentActivities(intent,PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
Log.d("Installed Applications", rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
Log.d("packegename",rInfo.activityInfo.applicationInfo.packageName.
toString());
}
如何获得每个应用程序使用的权限?
答案 0 :(得分:8)
所以我编码it.i不仅需要权限,还需要接收者和services.pls,请参阅以下代码,希望它对其他人有用。
PackageManager p = this.getPackageManager();
final List <PackageInfo> appinstall=p.getInstalledPackages(PackageManager.GET_PERMISSIONS|PackageManager.GET_RECEIVERS|
PackageManager.GET_SERVICES|PackageManager.GET_PROVIDERS);
for(PackageInfo pInfo:appinstall){
//PermissionInfo[] permission=pInfo.permissions;
String[] reqPermission=pInfo.requestedPermissions;
ServiceInfo[] services=pInfo.services;
ProviderInfo[] providers=pInfo.providers;
int versionCode=pInfo.versionCode;
Log.d("versionCode-package ",Integer.toString(versionCode));
Log.d("Installed Applications", pInfo.applicationInfo
.loadLabel(pm).toString());
Log.d("packegename",pInfo.packageName.
toString());
if(reqPermission!=null)
for(int i=0;i<reqPermission.length;i++)
Log.d("permission list",reqPermission[i]);
}
注意设置标志很重要,否则会导致问题无法获取服务,提供商 注意 - 无效检查是影响还是给予NPE
我之前使用activityInfo编写的代码这个使用packageInfo。现在我觉得更好:) 快乐的编码ppl :)
答案 1 :(得分:2)
此处如何检索Android设备上安装的应用列表以及每个应用使用的权限。
private static final String TAG = "MyActivity";
...
final PackageManager pm = getPackageManager();
final List<ApplicationInfo> installedApps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for ( ApplicationInfo app : installedApps ) {
//Details:
Log.d(TAG, "Package: " + app.packageName);
Log.d(TAG, "UID: " + app.uid);
Log.d(TAG, "Directory: " + app.sourceDir);
//Permissions:
StringBuffer permissions = new StringBuffer();
try {
PackageInfo packageInfo = pm.getPackageInfo(app.packageName, PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = packageInfo.requestedPermissions;
if ( requestedPermissions != null ) {
for (int i = 0; i < requestedPermissions.length; i++) {
permissions.append(requestedPermissions[i] + "\n");
}
Log.d(TAG, "Permissions: " + permissions);
}
}
catch ( PackageManager.NameNotFoundException e ) {
e.printStackTrace();
}
}
答案 2 :(得分:1)
// Loop through all installed packaged to get a list of used permissions and PackageInfos
for (PackageInfo pi : appList) {
// 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>();
try {
PermissionInfo pinfo = mPm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
CharSequence label = pinfo.loadLabel(mPm);
CharSequence desc = pinfo.loadDescription(mPm);
} catch (NameNotFoundException e) {
Log.i(TAG, "Ignoring unknown permission " + permission);
continue;
}
}
}
答案 3 :(得分:0)
这是我发现的唯一一件事,虽然我没有测试过。
让我知道它是否适用于任何人:
pm.getPackageInfo(rInfo.activityInfo.applicationInfo.packageName, packageManager.GET_PERMISSIONS);