Android获取设备隐私 忽略6.0权限管理
一.前言
(1).由于miui等部分国产定制系统也有权限管理,没有相关api,故无法判断用户是否允许获取联系人等隐私。在android 6.0之后,新增权限管理可以通过官方api判断用户的运行状态;
(2).我们指定targetsdkversion为23或者之后我们还需要在运行时请求这些所需的权限。这很重要,因为已经出现了很多开发者把targetsdkversion飙到了最新,然后发现自己的app疯狂的崩溃,这是由于他们没有实现执行运行时权限请求的代码。当你已经把一个targeting api 为23或者之后的app发布到了google play上,这更是一个问题,你无法立即把那个apk的targeting api替换成更早的版本。
二.权限分析
从android6.0开始,权限分为普通权限和许可权限。许可权限分类归组,一个权限授权之后,该组下的权限均可使用。
(1)普通权限
只需要在xml申请即可,使用方法和之前6.0以前的一样。在应用安装应用时,会默认获得许可。
(2)许可权限
可执行 $adb shell pm list permissions -d -g
同一组的任何一个权限被授权了,其他权限也自动被授权。例如,一旦write_contacts被授权了,app也有read_contacts和get_accounts了。
源码中被用来检查和请求权限的方法分别是activity的checkselfpermission和requestpermissions,这些方法api23引入。
三.相关方法
(1).contextcompat.checkselfpermission()
检查应用是否拥有该权限,被授权返回值为permission_granted,否则返回permission_denied
(2).activitycompat.requestpermissions()
将弹出请求授权对话框,这个方法在m之前版本调用,onrequestpermissionsresultcallback 直接被调用,带着正确的 permission_granted或者 permission_denied 。
(3).appcompatactivity.onrequestpermissionsresult()
该方法类似于activity的onactivityresult()的回调方法,主要接收请求授权的返回值
//版本判断 if (build.version.sdk_int >= 23) { //减少是否拥有权限 int checkcallphonepermission = contextcompat.checkselfpermission(getapplicationcontext(), permission); if (checkcallphonepermission != packagemanager.permission_granted) { //弹出对话框接收权限 activitycompat.requestpermissions(baseactivity.this, new string[]{permission}, id); return; }
@override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { super.onrequestpermissionsresult(requestcode, permissions, grantresults); if (grantresults[0] == packagemanager.permission_granted) { //todo:已授权 } else { //todo:用户拒绝 } }
(4).封装
public class baseactivity extends appcompatactivity { private map<integer, runnable> allowablepermissionrunnables = new hashmap<>(); private map<integer, runnable> disallowablepermissionrunnables = new hashmap<>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } /** * 请求权限 * @param id 请求授权的id 唯一标识即可 * @param permission 请求的权限 * @param allowablerunnable 同意授权后的操作 * @param disallowablerunnable 禁止权限后的操作 */ protected void requestpermission(int id, string permission, runnable allowablerunnable, runnable disallowablerunnable) { if (allowablerunnable == null) { throw new illegalargumentexception("allowablerunnable == null"); } allowablepermissionrunnables.put(id, allowablerunnable); if (disallowablerunnable != null) { disallowablepermissionrunnables.put(id, disallowablerunnable); } //版本判断 if (build.version.sdk_int >= 23) { //减少是否拥有权限 int checkcallphonepermission = contextcompat.checkselfpermission(getapplicationcontext(), permission); if (checkcallphonepermission != packagemanager.permission_granted) { //弹出对话框接收权限 activitycompat.requestpermissions(baseactivity.this, new string[]{permission}, id); return; } else { allowablerunnable.run(); } } else { allowablerunnable.run(); } } @override public void onrequestpermissionsresult(int requestcode, @nonnull string[] permissions, @nonnull int[] grantresults) { super.onrequestpermissionsresult(requestcode, permissions, grantresults); if (grantresults[0] == packagemanager.permission_granted) { runnable allowrun = allowablepermissionrunnables.get(requestcode); allowrun.run(); } else { runnable disallowrun = disallowablepermissionrunnables.get(requestcode); disallowrun.run(); } } }
public class mainactivity extends baseactivity implements view.onclicklistener{ private button btcallphone; private button btcontact; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btcallphone = (button) findviewbyid(r.id.call_phone); btcontact = (button) findviewbyid(r.id.contact); btcallphone.setonclicklistener(this); btcontact.setonclicklistener(this); } @override public void onclick(view v) { if(v == btcallphone){ //拨打电话 requestpermission(1, manifest.permission.call_phone, new runnable() { @override public void run() { callphone(); } }, new runnable() { @override public void run() { callphonedenied(); } }); }else if(v == btcontact){ //读取联系人信息 requestpermission(2, manifest.permission.write_contacts, new runnable() { @override public void run() { readcontact(); } }, new runnable() { @override public void run() { readcontactdenied(); } }); } } private void callphone() { toast.maketext(mainactivity.this, "call_phone ok", toast.length_short) .show(); } private void callphonedenied() { toast.maketext(mainactivity.this, "call_phone denied", toast.length_short) .show(); } private void readcontact() { contentresolver cr = getcontentresolver(); string str[] = {contactscontract.commondatakinds.phone.contact_id, contactscontract.commondatakinds.phone.display_name, contactscontract.commondatakinds.phone.number, contactscontract.commondatakinds.phone.photo_id}; cursor cur = cr.query( contactscontract.commondatakinds.phone.content_uri, str, null, null, null); int count = cur.getcount(); cur.close(); toast.maketext(mainactivity.this, string.format("发现%s条", count), toast.length_short) .show(); } private void readcontactdenied() { toast.maketext(mainactivity.this, "contact denied", toast.length_short) .show(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助。