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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux smartd failed

    linux smartd failed

    2021年8月6日
    53
  • linux内核发包工具,Linux内核发包工具pktgen测试方案说明「建议收藏」

    linux内核发包工具,Linux内核发包工具pktgen测试方案说明「建议收藏」简介pktgen是Linux内核里包含的一个高性能发包工具,主要用来测试网络性能。一般情况下,使用pktgen就可以满足千兆网卡的测试需要。pktgen运行在“内核态”,并不占用太多的系统资源,就可以达到非常高的发包速率。pktgen只支持UDP发包(端口9)。因为pktgen是一个非常底层测试工具,而且一般是测试网络设备的性能,并不涉及到应用层面。如果要测试高级的网络应用的性能,请使用其它的测…

    2025年9月19日
    6
  • SilverLight浏览器支持情况

    SilverLight浏览器支持情况推荐使用IE、360浏览器,搜狗、QQ浏览器需切换到兼容模式。以下浏览器版本不再支持:Chrome45及以后版本、Firefox52及以后版本、MicrosoftEdge浏览器、Opera浏览器。…

    2022年7月17日
    32
  • java StringTokenizer

    java StringTokenizerStringTokenizer是一个用来分隔String的应用类,相当于VB的split函数。1.构造函数publicStringTokenizer(Stringstr)publicStringTokenizer(Stringstr,Stringdelim)publicStringTokenizer(Stringstr,Stringdelim,boolean

    2022年8月11日
    6
  • NVIC的使用[通俗易懂]

    NVIC的使用[通俗易懂]NVIC相关内容中断编程的四个部分——1.使能中断请求;2.配置中断优先级分组(一个项目中只需要配置一次)3.配置NVIC寄存器,初始化NVIC_InitTypeDef;4.编写中断服务函数。中断过程的三个部分——1.配置NVIC_Config()函数:NVIC是嵌套向量中断控制器,控制着整个芯片中断相关的功能,它跟内核紧密耦合,是内核里面的一个外设。因此控制中断的进行与NVIC_Config函数的配置密切相关。2.配置EXTI_Config()函数:3.编写中断服务函数:…

    2022年5月27日
    56
  • ctk编译linux,CTK插件框架学习5-插件间通信(Netlink实现热拔插监控)[通俗易懂]

    ctk编译linux,CTK插件框架学习5-插件间通信(Netlink实现热拔插监控)[通俗易懂]本章来写一个插件,插件功能为通过NETLINK读取linux系统中的hotplug信息,比如usb、SD卡、磁盘等设备的插拔事件产生的信息,将读到的信息通过插件间通信的方式发出。1.eventadmin库编译CTKPluginFramework下插件间通信是通过事件管理机制实现的,其代码位于CTK/Libs/PluginFramework/service/event目录下,使能事件管理机制,…

    2022年5月29日
    46

发表回复

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

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