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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python基础(7)内置函数divmod用法

    python基础(7)内置函数divmod用法前言我们都知道,python中//代表整数运算中的取整,%代表整数运算中的取余,那么有什么函数可以同时取到整数和余数吗?答案是有的,使用python内置函数divmoddivmod首先看一下源

    2022年8月7日
    1
  • 安装win8、ubuntu双系统的过程

    弄了一个晚上,终于完成了,之前是用虚拟机的,但是觉得不带劲,并且折腾来时菜鸟变大神的捷径,虽然现在还一直在爬坑。继续奋斗吧…王小二首先是看ubuntu百度贴吧的安装帖子(http://tieb

    2021年12月20日
    40
  • java抽象类和抽象方法[通俗易懂]

    java抽象类和抽象方法[通俗易懂]1.抽象的概念2.抽象类和抽象方法的使用1//抽象方法和抽象类的格式:2/*抽象方法:就是加上abstract关键字,然后去掉大括号,直接分号结束;3抽象类:抽象方法所在的类,必须是抽象

    2022年7月3日
    26
  • python 文档注释快捷键,python注释快捷键是什么「建议收藏」

    python 文档注释快捷键,python注释快捷键是什么「建议收藏」python注释快捷键:1、单行注释是【#】,Mac的快捷键是【command+/】,windows的快捷键是【Ctrl+/】;2、多行注释是三个单引号【”’注释”’】。g1m少儿编程网-https://www.pxcodes.comg1m少儿编程网-https://www.pxcodes.com本教程操作环境:windows7系统、python3.9版,DELLG3电脑。g1m少儿编程…

    2022年5月12日
    55
  • Microsoft Enterprise Library: Logging 模块「建议收藏」

    Microsoft Enterprise Library: Logging 模块「建议收藏」MicrosoftEnterpriseLibrary中的Logging模块主要用来记录日志,它可以将日志存储在不同的介质中:文本文件,WindowsEvent,邮件,MSMQ,DataBase,Xml等等。当然它还提供了扩展功能,通过扩展Logging模块的Listener类,我们就能将日志记录在我们需要的地方了。   虽然MicrosoftEnterpriseLibrary很庞大,但

    2022年10月20日
    0
  • 语义分割最新指南2019版「建议收藏」

    语义分割最新指南2019版「建议收藏」之前出现的深度学习语义分割指南2017版(A2017GuidetoSemanticSegmentationwithDeepLearning),作者总结了当年各个具有代表性的语义分割算法与相关贡献。今年NamespaceAfrica数据科学家DerrickMwiti对该领域的再一次梳理(原文链接),希望对大家有帮助。语义分割指的是将图像中的每一个像素关联到一个类别标签上…

    2022年8月21日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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