const granted = PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE
)
granted.then((data)=>{
}).catch((err)=>{
this.show(err.toString())
})
} catch (err) {
this.show(err.toString())
}
}
//请求多个
async requestMultiplePermission() {
try {
const permissions = [
PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
PermissionsAndroid.PERMISSIONS.CAMERA
]
//返回得是对象类型
const granteds = await PermissionsAndroid.requestMultiple(permissions)
var data = "是否同意地址权限: "
if (granteds[“android.permission.ACCESS_FINE_LOCATION”] === “granted”) {
data = data + “是n”
} else {
data = data + “否n”
}
data = data+"是否同意相机权限: "
if (granteds[“android.permission.CAMERA”] === “granted”) {
data = data + “是n”
} else {
data = data + “否n”
}
data = data+"是否同意存储权限: "
if (granteds[“android.permission.WRITE_EXTERNAL_STORAGE”] === “granted”) {
data = data + “是n”
} else {
data = data + “否n”
}
this.show(data)
} catch (err) {
this.show(err.toString())
}
}
- 完整代码
import React,{Component} from ‘react’
import {
StyleSheet,
View,
Text,
TouchableOpacity,
ToastAndroid,
PermissionsAndroid,
} from ‘react-native’
export default class PermissionAndroidView extends Component {
render() {
return (
onPress={this.requestReadPermission.bind(this)}>
onPress={this.requestCarmeraPermission.bind(this)}>
onPress={this.requestLocationPermission.bind(this)}>
onPress={this.checkPermission.bind(this)}>
onPress={this.requestMultiplePermission.bind(this)}>
) } show(data) { ToastAndroid.show(data,ToastAndroid.SHORT) } async requestReadPermission() { try { //返回string类型 const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, { //第一次请求拒绝后提示用户你为什么要这个权限 ‘title’: ‘我要读写权限’, ‘message’: ‘没权限我不能工作,同意就好了’ } ) if (granted === PermissionsAndroid.RESULTS.GRANTED) { this.show(“你已获取了读写权限”) } else { this.show(“获取读写权限失败”) } } catch (err) { this.show(err.toString()) } } async requestCarmeraPermission() { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.CAMERA, { //第一次请求拒绝后提示用户你为什么要这个权限 ‘title’: ‘我要相机权限’, ‘message’: ‘没权限我不能工作,同意就好了’ } ) if (granted === PermissionsAndroid.RESULTS.GRANTED) { this.show(“你已获取了相机权限”) } else { this.show(“获取相机失败”) } } catch (err) { this.show(err.toString()) } } async requestLocationPermission() { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, { //第一次请求拒绝后提示用户你为什么要这个权限 ‘title’: ‘我要地址查询权限’, ‘message’: ‘没权限我不能工作,同意就好了’ } ) if (granted === PermissionsAndroid.RESULTS.GRANTED) { this.show(“你已获取了地址查询权限”) } else { this.show(“获取地址查询失败”) } } catch (err) { this.show(err.toString()) } } checkPermission() { try { //返回Promise类型 const granted = PermissionsAndroid.check( PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE ) granted.then((data)=>{ this.show(“是否获取读写权限”+data) }).catch((err)=>{ this.show(err.toString()) }) } catch (err) { this.show(err.toString()) } } async requestMultiplePermission() { try { const permissions = [ PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, PermissionsAndroid.PERMISSIONS.CAME 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》 【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享 RA ] //返回得是对象类型 const granteds = await PermissionsAndroid.requestMultiple(permissions) var data = "是否同意地址权限: " if (granteds[“android.permission.ACCESS_FINE_LOCATION”] === “granted”) { data = data + “是n” } else { data = data + “否n” } data = data+"是否同意相机权限: " if (granteds[“android.permission.CAMERA”] === “granted”) { data = data + “是n” } else { data = data + “否n” } data = data+"是否同意存储权限: " if (granteds[“android.permission.WRITE_EXTERNAL_STORAGE”] === “granted”) { data = data + “是n” } else { data = data + “否n” } this.show(data) } catch (err) { this.show(err.toString()) } } } const styles = StyleSheet.create({ container: { flex: 1, padding: 10, }, button_view: { margin:4, borderRadius: 4, backgroundColor: ‘#8d4dfc’, alignItems: ‘center’, }, button_text: { 欢迎分享,转载请注明来源:内存溢出
padding: 6,
fontSize: 16,
fontWeight: ‘600’
}
})
评论列表(0条)