Android手动检查并申请权限方法
作者:Suzi丶 发布时间:2023-08-04 23:14:17
标签:Android,权限
Android权限一般是在AndroidManifest.xml中声明,在安装或首次使用的时候系统会自动提示用户是否提供权限
Android官方文档:
Then, when the application is installed on the device, the installer determines whether or not to grant the requested permission by checking the authorities that signed the application's certificates and, in some cases, asking the user. If the permission is granted, the application is able to use the protected features. If not, its attempts to access those features will simply fail without any notification to the user.
但有时候因为手机设备等原因,并没有在一开始就弹出提示框让用户确认是否提供权限,对于有些权限,不确认的话就不提供权限,因此,有些功能就无法实现。
这时候需要我们手动检查是否已经拥有权限,没有的话就调用代码提示用户提供权限。
如何手动检查并申请权限
如:在写入之前检查是否有WRITE_EXTERNAL_STORAGE权限,没有则申请权限
代码:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//申请WRITE_EXTERNAL_STORAGE权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
WRITE_EXTERNAL_STORAGE_REQUEST_CODE);
}
请求权限后,系统会弹出请求权限的Dialog,允许即可
用户选择允许或拒绝后,会回调onRequestPermissionsResult方法, 该方法类似于onActivityResult
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
doNext(requestCode,grantResults);
}
PS:
在Android6.0,有些权限只需要在AndroidManifest.xml文件中声明即可,但有些除了声明以外,还需要在运行时判断,即用户确认,需要确认的权限如下:
身体传感器
日历
摄像头
通讯录
地理位置
麦克风
电话
短信
存储空间
Android6.0系统默认为targetSdkVersion小于23的应用默认授予了所申请的所有权限,
所以如果你以前的APP设置的targetSdkVersion低于23,在运行时也不会崩溃,
但这也只是一个临时的救急策略,用户还是可以在设置中取消授予的权限。
以下权限只需要在AndroidManifest.xml中声明即可使用:
android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT
来源:https://blog.csdn.net/Suzi1203/article/details/51607714
0
投稿
猜你喜欢
- 1.背景在项目中有些敏感信息不能直接展示,比如客户手机号、身份证、车牌号等信息,展示时均需要进行数据脱敏,防止泄露客户隐私。脱敏即是对数据的
- 前言在Android屏幕的空间中,大部分的区域我们都是可以随意绘制,只有一部分区域是显示的固定内容:状态栏标题栏(ActionBar)页面内
- 有时候我们从数据库中查询出来数据之后,需要按照DataTable的某列进行分组,可以使用下面的方法实现,代码如下:using System;
- yaml中的数组类型取值yaml中简单的风格,十分受大家的欢迎先说下简单的配置如何取值# application-dev.yml testV
- App中,有很大一部分场景是点击按钮,向服务端提交数据,由于网络请求需要时间,用户很可能会多次点击,造成数据重复提交,造成各种莫名其妙的问题
- 最近做一个C#项目,需要对radis进行读写。首先引入System.Configuration,如下实现代码如下:public class
- 看看效果图:我们项目中头像显示一般都是圆形的,但是有时候不排除各种样式(不一定是个规则的形状),比如 上次UI给了我一个 圆形下面少了一块。
- LinkedList实现班级信息管理系统,供大家参考,具体内容如下代码如下:import java.util.*;public class
- 前言本文基于 RxJava 和 Retrofit 库,设计并实现了一种用于大文件分块上传的工具,并对其进行了全面的拆解分析。抛砖引玉,对同样
- 字符串和列表学完,自己试着写了一个非常简单的Python名片管理系统。新萌尝试,大佬们不要喷。修改名片的功能我偷了个懒,因为我不知道怎么通过
- 分栏是报刊、书籍、杂志常用的排版样式,它不仅能方便阅读,同时也能增加页面的美观度。本文将介绍如何在Java应用程序中给Word文档添加多个栏
- JMM与问题引入为啥先说JMM,因为CAS的实现类中维护的变量都被volatile修饰, 这个volatile 是遵循JMM规范(不是百分百
- //********************************************************** //******主
- Android 资源 id详解我们平时获取资源是通过 findViewById 方法进行的,比如我们常在onCreate方法中使用这样的语句
- 每年实验课,总有同学问我,如何生成DLL、如何导出类,如何不花很多时间精力,就设计出一个给别人用的爽的功能库呢?结合这些年的实践,我们今天就
- 23种设计模式第十四篇:java迭代器模式定义:提供一种方法访问一个容器对象中各个元素,而又不暴露该对象的内部细节。类型:行为类模式类图:如
- 360首页搜索效果如下1、完成编写的schoolnet校园网主要目录结构如下主要实现支持中文、拼音首字母、拼音全字母的智能搜索和换肤页面效果
- like模糊查询特殊字符报错转义处理方案1 <if test="projectName!
- LeetCode54. 螺旋矩阵 java实现题目难度 中给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,
- 起因最近在写CRUD的时候,发现有个分页的VO写的健壮性比较差,一时手痒改了一下,没想到改了之后好几个功能都出现了问题。原VO关键代码如下: