本文共 14341 字,大约阅读时间需要 47 分钟。
PackageManager中提供的武器,可以用“既多又杂,版本变化大”来形容。
不过,我们通过分类和排序的方法将它们组织起来,让开发者同学们可以学会使用这些强大的武器!这一节我们将学习:
我们先看第一个大族的API,Permission相关的API
创始成员7个. Android 2.2版新增1个,Android 4.3新增1个,Android 6.0再增1个。共计10个。
一共七个,基本上是权限的增,删,检查和查询4种操作。
原型:
ListgetAllPermissionGroups (int flags);
参数:
真棒,只有一个选择:GET_META_DATA。我们写一段例程看看效果:
public void testGetAllPermissionGroups(){ Listlist = mPm.getAllPermissionGroups(PackageManager.GET_META_DATA); if(list!=null){ for(PermissionGroupInfo pgi: list){ Log.d(TAG, "PermissionGroupInfo:" + pgi.toString()); } } }
日志结果如下:
8-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{61443ad android.permission-group.CONTACTS flgs=0x0}08-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{80db6e2 android.permission-group.PHONE flgs=0x0}08-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{5bea73 android.permission-group.APPSTORE_CLOUD flgs=0x0}08-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{840d30 android.permission-group.CALENDAR flgs=0x0}08-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{75e6da9 android.permission-group.CAMERA flgs=0x0}08-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{2545f2e android.permission-group.SENSORS flgs=0x0}08-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{e80bacf android.permission-group.LOCATION flgs=0x0}08-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{d4cc5c android.permission-group.STORAGE flgs=0x0}08-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{c073b65 android.permission-group.MICROPHONE flgs=0x0}08-11 15:49:16.308 26087-26087/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{84b403a android.permission-group.SMS flgs=0x0}
可见,我当前所用的Android M手机上,有下面这些PerssionGroup:
这其中用到的PermissionGroupInfo类,用于来存储权限组的信息. 我们后面专门有相关数据结构类的详细说明。
根据字符串来查询特定的PerssionGroupInfo。
例程:
public void testGetPermissionGroupInfo(){ try { PermissionGroupInfo pgi = mPm.getPermissionGroupInfo("android.permission-group.CONTACTS", PackageManager.GET_META_DATA); if(pgi!=null){ Log.d(TAG,"PermissionGroup description is:"+pgi.loadDescription(mPm)); } }catch(PackageManager.NameNotFoundException e){ Log.e(TAG,""); } }
通过上面两个方法的学习,我们已经可以获取PermissionGroup的信息了。那么,我们如何去知道一个权限组里都有哪些权限呢?
我们可以通过queryPermissionsByGroup方法来实现这个需求,请看例程:public void testQueryPermissionsByGroup(){ try { Listlist = mPm.queryPermissionsByGroup("android.permission-group.CONTACTS", PackageManager.GET_META_DATA); if(list!=null){ for(PermissionInfo pi : list){ Log.d(TAG,"Permission info:"+pi); Log.d(TAG,"Permission Description is:"+pi.loadDescription(mPm)); } } }catch(PackageManager.NameNotFoundException e){ Log.e(TAG,"testQueryPermissionsByGroup error",e); } }
输出的结果如下:
08-11 16:35:51.230 28164-28164/? D/TestPackageManager: Permission info:PermissionInfo{b8fe1ce android.permission.WRITE_CONTACTS}08-11 16:35:51.231 28164-28164/? D/TestPackageManager: Permission Description is:允许该应用修改您手机上存储的联系人的相关数据,包括您通过打电话、发送电子邮件或以其他方式与特定联系人通信的频率。此权限可让应用删除联系人数据。08-11 16:35:51.231 28164-28164/? D/TestPackageManager: Permission info:PermissionInfo{66418ef android.permission.GET_ACCOUNTS}08-11 16:35:51.232 28164-28164/? D/TestPackageManager: Permission Description is:允许该应用获取手机已知的帐户列表,其中可能包括由已安装的应用创建的所有帐户。08-11 16:35:51.232 28164-28164/? D/TestPackageManager: Permission info:PermissionInfo{119ffc android.permission.READ_CONTACTS}08-11 16:35:51.232 28164-28164/? D/TestPackageManager: Permission Description is:允许该应用读取您手机上存储的联系人的相关数据,包括您通过打电话、发送电子邮件或以其他方式与特定个人通信的频率。此权限可让应用保存您的联系人数据,而恶意应用可能会在您不知情的情况下分享联系人数据。
从上面的Log上我们可以看到,android.permission-group.CONTACTS组里有3个成员:
根据名字查找对应的权限对象,不多说了,上例程:
public void testGetPermissionInfo() { try { PermissionInfo pi = mPm.getPermissionInfo("android.permission.WRITE_CONTACTS", PackageManager.GET_META_DATA); Log.d(TAG,"Permission Description is:"+pi.loadDescription(mPm)); } catch (PackageManager.NameNotFoundException e) { Log.d(TAG,"testGetPermissionInfo error",e); } }
输出如下:
08-11 16:53:58.548 28325-28325/? D/TestPackageManager: Permission Description is:允许该应用读取您手机上存储的联系人的相关数据,包括您通过打电话、发送电子邮件或以其他方式与特定个人通信的频率。此权限可让应用保存您的联系人数据,而恶意应用可能会在您不知情的情况下分享联系人数据。0
检查是否一个包是否被允许某个权限。看例程:
public void testCheckPermission(){ final int i = mPm.checkPermission("android.permission.WRITE_CONTACTS", "com.yunos.xulun.testcppjni2"); if(i== PackageManager.PERMISSION_GRANTED){ Log.d(TAG,"Permission Granted!"); }else{ Log.d(TAG,"Permission Denied!"); } }
addPermission和removePermission两个方法暂时没见到有人用到,先略过不讲了。
例程:
public void testGetPackagesHoldingPermissions(){ Listlist = mPm.getPackagesHoldingPermissions(new String[]{"android.permission.WRITE_CONTACTS"},PackageManager.GET_META_DATA | PackageManager.GET_PROVIDERS); if(list!=null){ for(PackageInfo pi : list){ Log.d(TAG,"Package that use WRITE_CONTACTS:"+pi.packageName); } } }
输出吓一跳,怎么这么多应用要改联系人!
08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.gba08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.ims08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.ppl08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.providers.telephony08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.schpwronoff08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.ygps08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.mms.service08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.engineermode08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.wfo.impl08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.tencent.mm08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:android08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.connectivity08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.ftprecheck08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.mms08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.stk08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.providers.settings08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.batterywarning08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.incallui08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.inputdevices08-11 17:22:49.773 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.nlpservice08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.server.telecom08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.keychain08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.atci.service08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mediatek.thermalmanager08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.mtk.telephony08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.settings08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.vpndialogs08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.phone08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.shell08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.location.fused08-11 17:22:49.774 28747-28747/? D/TestPackageManager: Package that use WRITE_CONTACTS:com.android.bluetooth
公开域:
其实loadDescription中早已经考虑到上面所讲的两种情况了,调用loadDescription一定能得到描述的字符串:
public CharSequence loadDescription(PackageManager pm) { if (nonLocalizedDescription != null) { return nonLocalizedDescription; } if (descriptionRes != 0) { CharSequence label = pm.getText(packageName, descriptionRes, null); if (label != null) { return label; } } return null; }
我们还是通过一个例程来学习下,可以读取到权限的本地语言,如中文的描述,还是挺好玩的哈:
public void testGetAllPermissionGroups(){ Listlist = mPm.getAllPermissionGroups(PackageManager.GET_META_DATA); if(list!=null){ for(PermissionGroupInfo pgi: list){ Log.d(TAG, "PermissionGroupInfo:" + pgi.toString()); if(TextUtils.isEmpty(pgi.nonLocalizedDescription)){ Log.d(TAG,"PermissionGroup description is:"+pgi.loadDescription(mPm)); }else{ Log.d(TAG,"PermissionGroup description is:"+pgi.nonLocalizedDescription); } } } }
输出的结果如下:
08-11 16:13:05.089 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{61443ad android.permission-group.CONTACTS flgs=0x0}08-11 16:13:05.091 26875-26875/? D/TestPackageManager: PermissionGroup description is:使用您的通讯录08-11 16:13:05.092 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{80db6e2 android.permission-group.PHONE flgs=0x0}08-11 16:13:05.092 26875-26875/? D/TestPackageManager: PermissionGroup description is:拨打电话和管理通话08-11 16:13:05.092 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{5bea73 android.permission-group.APPSTORE_CLOUD flgs=0x0}08-11 16:13:05.092 26875-26875/? D/TestPackageManager: PermissionGroup description is:null08-11 16:13:05.092 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{840d30 android.permission-group.CALENDAR flgs=0x0}08-11 16:13:05.093 26875-26875/? D/TestPackageManager: PermissionGroup description is:访问您的日历08-11 16:13:05.093 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{75e6da9 android.permission-group.CAMERA flgs=0x0}08-11 16:13:05.094 26875-26875/? D/TestPackageManager: PermissionGroup description is:拍摄照片和录制视频08-11 16:13:05.094 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{2545f2e android.permission-group.SENSORS flgs=0x0}08-11 16:13:05.095 26875-26875/? D/TestPackageManager: PermissionGroup description is:访问与您的生命体征相关的传感器数据08-11 16:13:05.095 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{e80bacf android.permission-group.LOCATION flgs=0x0}08-11 16:13:05.096 26875-26875/? D/TestPackageManager: PermissionGroup description is:使用此设备的位置信息08-11 16:13:05.096 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{d4cc5c android.permission-group.STORAGE flgs=0x0}08-11 16:13:05.097 26875-26875/? D/TestPackageManager: PermissionGroup description is:访问您设备上的照片、媒体内容和文件08-11 16:13:05.097 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{c073b65 android.permission-group.MICROPHONE flgs=0x0}08-11 16:13:05.097 26875-26875/? D/TestPackageManager: PermissionGroup description is:录制音频08-11 16:13:05.097 26875-26875/? D/TestPackageManager: PermissionGroupInfo:PermissionGroupInfo{84b403a android.permission-group.SMS flgs=0x0}08-11 16:13:05.098 26875-26875/? D/TestPackageManager: PermissionGroup description is:发送和查看短信
转载地址:http://tkcgo.baihongyu.com/