Read all the information of installed packages
package com.example.phonesecurity;
import java.util.List;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PermissionGroupInfo;
import android.content.pm.PermissionInfo;
import android.util.Log;
public class PermissionManager {
/**
* example:
* Package Name: com.Yang
* Application Name: UITest
* android.permission.WRITEEXTERNAL_STORAGE
*
* Package Name: com.hfq.work
* Application Name:Phonesecman
* android.permission.READ_PHONE_STATE
*
* .......
* @return all information of packages
*/
private StringBuilder getAllPermission(Context context)
{
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> list = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
StringBuilder stringBuilder = new StringBuilder();
for (PackageInfo packageInfo : list) {
// stringBuilder.append("package name:" + packageInfo.packageName
// + "\n");
stringBuilder.append("package name:" + packageInfo.packageName + "\n");
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
stringBuilder.append("应用名称:" + applicationInfo.loadLabel(packageManager)
+ "\n");
if(getBriefPermission(context, packageInfo.packageName)!=null)
stringBuilder.append(getBriefPermission(context, packageInfo.packageName).toString());
stringBuilder.append("\n");
}
return stringBuilder;
}
/**
* /**
* example:
* Package Name: com.Yang
* Application Name: UITest
* android.permission.WRITEEXTERNAL_STORAGE
*
* @param context
* @param packageName
* @return the permission of a certain package name
*/
private StringBuilder getBriefPermission(Context context, String packageName) {
StringBuilder stringBuilder = new StringBuilder();
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
// 得到自己的包名
String pkgName = pi.packageName;
PackageInfo pkgInfo = pm.getPackageInfo(pkgName,
PackageManager.GET_PERMISSIONS);// 通过包名,返回包信息
String sharedPkgList[] = pkgInfo.requestedPermissions;// 得到权限列表
if (sharedPkgList == null)
return null;
for (int i = 0; i < sharedPkgList.length; i++) {
String permName = sharedPkgList[i];
PermissionInfo tmpPermInfo = pm.getPermissionInfo(permName, 0);// 通过permName得到该权限的详细信息
PermissionGroupInfo pgi = pm.getPermissionGroupInfo(
tmpPermInfo.group, 0);// 权限分为不同的群组,通过权限名,我们得到该权限属于什么类型的权限。
stringBuilder.append(permName + "\n");
//tv.append(permName + "\n");
// tv.append(i + "-" + permName + "\n");
// tv.append(i + "-" + pgi.loadLabel(pm).toString() + "\n");
// tv.append(i + "-" + tmpPermInfo.loadLabel(pm).toString()+
// "\n");
// tv.append(i + "-" +
// tmpPermInfo.loadDescription(pm).toString()+ "\n");
// tv.append("\n");
}
} catch (NameNotFoundException e) {
Log.e("##ddd", "Could'nt retrieve permissions for package");
}
return stringBuilder;
}
/**
*
* @param context
* @param packageName
* @return detailed permission of a certain package name
*/
private StringBuilder getFullPermission(Context context, String packageName) {
StringBuilder stringBuilder = new StringBuilder();
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
// 得到自己的包名
String pkgName = pi.packageName;
PackageInfo pkgInfo = pm.getPackageInfo(pkgName,
PackageManager.GET_PERMISSIONS);// 通过包名,返回包信息
String sharedPkgList[] = pkgInfo.requestedPermissions;// 得到权限列表
for (int i = 0; i < sharedPkgList.length; i++) {
String permName = sharedPkgList[i];
PermissionInfo tmpPermInfo = pm.getPermissionInfo(permName, 0);// 通过permName得到该权限的详细信息
PermissionGroupInfo pgi = pm.getPermissionGroupInfo(
tmpPermInfo.group, 0);// 权限分为不同的群组,通过权限名,我们得到该权限属于什么类型的权限。
stringBuilder.append(i + "-" + permName + "\n");
stringBuilder.append(i + "-" + pgi.loadLabel(pm).toString() + "\n");
stringBuilder.append(i + "-" + tmpPermInfo.loadLabel(pm).toString() + "\n");
stringBuilder.append(i + "-" + tmpPermInfo.loadDescription(pm).toString()
+ "\n");
stringBuilder.append("\n");
/*tv.append(i + "-" + permName + "\n");
tv.append(i + "-" + pgi.loadLabel(pm).toString() + "\n");
tv.append(i + "-" + tmpPermInfo.loadLabel(pm).toString() + "\n");
tv.append(i + "-" + tmpPermInfo.loadDescription(pm).toString()
+ "\n");
tv.append("\n");*/
}
} catch (NameNotFoundException e) {
Log.e("##ddd", "Could'nt retrieve permissions for package");
}
return stringBuilder;
}
/**
*
* @param permissionName
* @return all package names and application names when one of their permissions hits the 'permissionName"
*/
private StringBuilder getInfoWithCertainPermission(Context context, String permissionName)
{
boolean flag=false;
String permission=null;
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> list = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
StringBuilder stringBuilder = new StringBuilder();
for (PackageInfo packageInfo : list) {
// stringBuilder.append("package name:" + packageInfo.packageName
// + "\n");
if(getBriefPermission(context, packageInfo.packageName)!=null)
{
permission=getBriefPermission(context, packageInfo.packageName).toString();
if(permission.contains(permissionName))
{
flag=true;
}
//stringBuilder.append(getBriefPermission(this, packageInfo.packageName).toString());
}
if(flag)
{
stringBuilder.append("package name:" + packageInfo.packageName + "\n");
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
stringBuilder.append("应用名称:" + applicationInfo.loadLabel(packageManager)
+ "\n");
stringBuilder.append("\n");
}
flag=false;
}
return stringBuilder;
}
/*
public StringBuilder showPackageWithCertainPermission(String permissionName) {
boolean flag = false;
PackageManager packageManager = getPackageManager();
List<PackageInfo> list = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
StringBuilder stringBuilder = new StringBuilder();
for (PackageInfo packageInfo : list) {
if (packageInfo.permissions != null) {
for (PermissionInfo p : packageInfo.permissions) {
if (p.name.equals(permissionName))
flag = true;
}
if (flag) {
stringBuilder.append("package name:"
+ packageInfo.packageName + "\n");
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
stringBuilder.append("应用名称:"
+ applicationInfo.loadLabel(packageManager) + "\n");
stringBuilder.append("\n");
}
}
flag = false;
}
// tv.setText(stringBuilder.toString());
// tv.setText(stringBuilder.toString());
return stringBuilder;
}
public StringBuilder showAllInfo() {
PackageManager packageManager = getPackageManager();
List<PackageInfo> list = packageManager
.getInstalledPackages(PackageManager.GET_PERMISSIONS);
StringBuilder stringBuilder = new StringBuilder();
for (PackageInfo packageInfo : list) {
stringBuilder.append("package name:" + packageInfo.packageName
+ "\n");
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
stringBuilder.append("应用名称:"
+ applicationInfo.loadLabel(packageManager) + "\n");
if (packageInfo.permissions != null) {
for (PermissionInfo p : packageInfo.permissions) {
stringBuilder.append("权限包括:" + p.name + "\n");
}
}
stringBuilder.append("\n");
}
// tv.setText(stringBuilder.toString());
tv.setText(stringBuilder.toString());
return stringBuilder;
}*/
}
ListView
http://www.iteye.com/topic/540423