主要通过PackageManager中的方法
代码:
public class InstallApps extends ListActivity {
HashMap map=new HashMap();
List packagelist=new ArrayList();
List systemApp=new ArrayList();
protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PackageManager pm=getPackageManager();
//List mPackageinfo= pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);
Intent query=new Intent(Intent.ACTION_MAIN);
query.addCategory("android.intent.category.LAUNCHER");
List resolves= pm.queryIntentActivities(query, PackageManager.GET_ACTIVITIES);
for (int i = 0; i < resolves.size(); i++) {
ResolveInfo info= resolves.get(i);
/ /判断是否为系统级应用
if((info.activityInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0){
/*安装的应用*/
String packagename=info.loadLabel(pm).toString();
String[] permission;
try {
permission = pm.getPackageInfo(info.activityInfo.packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;//获取权限列表
packagelist.add(packagename);
map.put(packagename, permission);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else{
/*系统应用*/
String packagename=info.loadLabel(pm).toString();
String[] permission;
try {
permission = pm.getPackageInfo(info.activityInfo.packageName, PackageManager.GET_PERMISSIONS).requestedPermissions;//获取权限列表
systemApp.add(packagename);
map.put(packagename, permission);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/* 判断是否属于系统应用
for (int i = 0; i < mPackageinfo.size(); i++) {
PackageInfo info=mPackageinfo.get(i);
if((info.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)<=0){
String packagename=info.applicationInfo.loadLabel(pm).toString();
String[] permission= info.requestedPermissions;
packagelist.add(packagename);
map.put(packagename, permission);
} else{
String packagename=info.applicationInfo.loadLabel(pm).toString();
String[] permission= info.requestedPermissions;
systemApp.add(packagename);
map.put(packagename, permission);
}
}*/
packagelist.addAll(systemApp);
getListView().setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, packagelist));
getListView().setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> arg0, View arg1, int position, long arg3) {
// TODO Auto-generated method stub
String[]permission=map.get(packagelist.get(position));
StringBuilder sb=new StringBuilder();
for (int i = 0; i < permission.length; i++) {
sb.append("权限"+permission[i]+"\n");
}
Toast.makeText(getApplicationContext(), sb,Toast.LENGTH_LONG ).show();
}
});
};
}