React-Native之Android(6,移动开发专业

React-Native之Android(6,移动开发专业,第1张

React-Native之Android(6,移动开发专业

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.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’
}
})

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5660992.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存