targetSdkVersion 23以下6.0中调用requestPermissions()的问题

targetSdkVersion 23以下6.0中调用requestPermissions()的问题

targetSdkVersion如果是23以下,调用ActivityCompat.requestPermissions(),会弹出权限选择对话框,但是选择拒绝授权,onRequestPermissionsResult中的返回值却是PERMISSION_GRANTED,但选择同意授权,会把应用关闭重新开启当前activity,而不会调用onRequestPermissionsResult中的方法,所以不要在targetSdkVersion设置为23以下,又把complierSdkversion设置为23,这样会出现上述的问题。最好的方式是把targetSdkVersion也设置为23,就可以解决。一切完美运行。

下面是一些对应的解答:


The following are the Marshmallow permissions features every developer should keep in mind.

  1. As you may already know, all permissions are divided into two categories: normal and dangerous. Android 5.1 and older versions display dangerous permissions during installation. If a user denies them an application will not be installed at all. Starting from Android 6.0 dangerous permissions are divided into subcategories (see below), that should be separately confirmed by a user while using an app. You should remember though that if one permission from a group has been accepted, you will not be asked for additional confirmation of further ones that belong to it too.

image

  1. If you specify targetSdkVersion = 22 and lower in the build.gradle file while building-up an app, you will see a usual permission confirmation dialog during its installation from the Play Store to a device running Marshmallow. All permission groups will be turned on in settings after installation. If you try to turn off the app access to one of the needed permissions, the following dialog will be displayed:

image

  1. In case build.gradle targetSdkVersion = 23, you will need to add an access request to necessary permissions and user response processing. Let’s dig deeper into details here.

If you need access to the elements that require permissions, always check it with the help of the method:

ContextCompat.checkSelfPermission(activity,Manifest.permission.< verifiable_permission >

It is strongly recommended to use the ContextCompat class method  from the support-v4 library for verification, as Context.checkSelfPermission is available starting from Android 6.0 only and will cause crashes in the previous OS versions.

If this method returns the value PackageManager.PERMISSION_GRANTED, it means the permission has been confirmed and can be used now.

Otherwise, you need to request permission from a user. To do this, call the ActivityCompat.requestPermissions method and transfer the reference to Activity, the string array of necessary permissions and the request code as settings. If there have been no access requests to the needed permissions before, you will see a standard dialog that cannot be customized:

image

There is an onRequestPermissionsResult method added in Android 6.0 to process the call results. It works when a user pushes Deny or Allow buttons.

If a user presses Allow, access is confirmed and the app can go on running. No surprise about that. But what if not? In this case, further calls of ActivityCompat.requestPermissions will not display the dialog. Instead of this a developer is recommended to show a dialog with descriptions why the app needs the requested permissions. Use the ActivityCompat.shouldShowRequestPermissionRationale method and transfer the reference to Activity and permission there to check if the description is to be shown. If the method returns true, display the description and ask for access permission from a user again. This time the dialog will look different:

image

If a user presses Deny in this dialog and chooses Never ask again, all further calls of ActivityCompat.shouldShowRequestPermissionRationale will return false, and the ActivityCompat.requestPermissions call will not display the permission dialog. PERMISSION_DENIED will immediately return to onRequestPermissionResult instead.

There is a possibility to request a few permissions at a time, but official guidelines do not recommend to do that, except the case when a few permissions are required to appear on the same screen simultaneously.

As stated above, the ActivityCompat.requestPermissions method accepts the reference to Activity and the string array of necessary permissions as input parameters. You can indicate a few needed permissions in this string array and thus a user will see a separate dialog for each of them.

image

image

As a result, access parameters for each requested permission will be transferred to the onRequestPermissionsResult method.

This all is actually one more additional task to include into the general scope of application development activities but such individual permissions are more secure. Android gives its users the possibility to control and personalize the OS on their devices so don’t forget about that when creating apps for its Marshmallow and up versions.


转载于:https://my.oschina.net/u/990728/blog/550693

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/109194.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • cmd dos命令怎么查看进程,删除指定进程

    cmd dos命令怎么查看进程,删除指定进程cmd dos命令怎么查看进程,删除指定进程

  • 隐马尔科夫模型(hmm)_隐马尔可夫模型是什么意思

    隐马尔科夫模型(hmm)_隐马尔可夫模型是什么意思https://www.cnblogs.com/skyme/p/4651331.html

  • Redis缓存穿透、缓存雪崩问题分析

    Redis缓存穿透、缓存雪崩问题分析把redis作为缓存使用已经是司空见惯,但是使用redis后也可能会碰到一系列的问题,尤其是数据量很大的时候,经典的几个问题如下:(一)缓存和数据库间数据一致性问题分布式环境下(单机就不用说了)非常容易出现缓存和数据库间的数据一致性问题,针对这一点的话,只能说,如果你的项目对缓存的要求是强一致性的,那么请不要使用缓存。我们只能采取合适的策略来降低缓存和数据库间数据不一致的概率,而无法保证两…

  • UDP协议开发

    UDP协议开发1简介在进行电网插件开发的过程中,对电网接入程序进行了开发,使得在综合安防管理平台上能够非常方便的接入天地维正电网设备。电网数据采用UDP协议,通过监狱局域网,向用户指定的5个IP地址的某端口,同时发送,各IP地址收到的数据相同。因为是第一次使用网络数据报进行开发,因此遇到了许多的坑。在这里把遇到的问题组织成一个文档,重新理解在代码撰写过程中遇到的问题。本文档适用于初次使用UDP进行…

  • maven配置环境变量失败解决办法

    maven配置环境变量失败解决办法配置maven路径什么的统统正确,最后测hi不成功。在网上搜索了好多资料方法都解不了具体问题具体对待吧,如果有和我类似的小伙伴,可以尝试一下我的这个办法,在maven路径后面加/binpath变量中测试输入mva-v(中间有个空格)成功!如果还是不能解决也可以参这个https://blog.csdn.net/xudaliang_…

  • 卡盟网站程序源码,支持无限分站,货源共享「建议收藏」

    卡盟网站程序源码,支持无限分站,货源共享「建议收藏」服务器系统:cenos7.x64位(购买vps都知道ba?安装7.x以上系统)宝塔安装命令:bt.cn(自行获取)(baota我已经安装好了,接下来给大家演示如何操作搭建卡信乐)ssh输入宝塔命令会自动安装好自己查看一下登陆地址和账号密码就可以登陆面板后安装apache2.4+mysql5.5+php5.6phpadmin4.7(应该都会安装把!)登陆宝塔,打开网站的根目录的home文件夹,上传源码然后解压上传好后解压文件修改vhost文件里的域名创建一个数据库yunos数据库

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号