其他分享
首页 > 其他分享> > React-Native之Android(6,androidapp开发从入门到精通

React-Native之Android(6,androidapp开发从入门到精通

作者:互联网

//就举一个例子 记得加上async异步

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())

}

}

//核实

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.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 (

<TouchableOpacity style={styles.button_view}

onPress={this.requestReadPermission.bind(this)}>

申请读写权限

<TouchableOpacity style={styles.button_view}

onPress={this.requestCarmeraPermission.bind(this)}>

申请相机权限

<TouchableOpacity style={styles.button_view}

onPress={this.requestLocationPermission.bind(this)}>

申请访问地址权限

<TouchableOpacity style={styles.button_view}

onPress={this.checkPermission.bind(this)}>

查询是否获取了读写权限

<TouchableOpacity style={styles.button_view}

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)

(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)

标签:err,show,granted,PermissionsAndroid,androidapp,React,Android,权限,data
来源: https://blog.csdn.net/m0_66264673/article/details/122639333