简单整理一下packageManager的几个获取包信息的方法。下面的都是自己试验出来的
/**
* 获取本机中所有在AndroidManefest.xml中定义过的Activity的所有信息,包括permission,theme,name等等
* 只要是在AndroidManefest.xml中的<Activity>标签中能填写的属性,这里就可以获取。由ActivityInfo的源码得知
* toString函数只是打印了一个activity的编号和他的名字。
*
*/
private void showActivityInfo() {
for (PackageInfo pack : getPackageManager().getInstalledPackages(
PackageManager.GET_ACTIVITIES)) {
ActivityInfo[] activityInfos = pack.activities;
// Log.d(TAG, "pack.packageName =qqqq= " + pack.packageName
// + " pack.versionName =qqqq= " + pack.versionName
// + " versionCode ==qqq= " + pack.versionCode);
if (activityInfos != null) {
for (ActivityInfo activityInfo : activityInfos) {
Log.d(TAG, "activityInfos: " + activityInfo.toString()
+ " permission: " + activityInfo.permission);
}
}
}
}
/**
* 获取所有应用在AndroidManefest.xml中定义的Provider的信息,包括authority等。 与showActivity类似
*/
private void showProviderInfo() {
for (PackageInfo pack : getPackageManager().getInstalledPackages(
PackageManager.GET_PROVIDERS)) {
ProviderInfo[] providerInfos = pack.providers;
// Log.d(TAG, "pack.packageName == " + pack.packageName
// + " pack.versionName == " + pack.versionName
// + " versionCode === " + pack.versionCode);
if (providerInfos != null) {
for (ProviderInfo providerInfo : providerInfos) {
Log.d(TAG, "providerInfo: " + providerInfo.toString()
+ " authority: " + providerInfo.authority);
}
}
}
}
/**
* 获取手机中所有包,并打印出这些包都用到了哪些permission
*/
private void showPermission() {
for (PackageInfo pack : getPackageManager().getInstalledPackages(
PackageManager.GET_PERMISSIONS)) {
if (pack.requestedPermissions != null) {
Log.d(TAG, "*******************************************");
Log.d(TAG, "pack.packageName: " + pack.packageName);
for (String per : pack.requestedPermissions) {
Log.d(TAG, "permission ----- > " + per);
}
}
}
}
此乃原创,转载请注明出处。