Why Android to apply for permission
In Android6.0 and above, permissions that Google considers “dangerous and privacy” should not only be requested in the manifest file (bot/app/SRC/androidmainfest.xml), but also in a separate API that allows users to choose whether or not to allow you to apply for these permissions.
Such as: You want your application to have access to your phone’s external memory card, If you have android {defaultConfig {targetSdkVersion 23}} targetSdkVersion> = in your (bot/app/build.gradle) 23 You need to apply for permissions dynamically. I found that targetSdkVersion = 22 in the React-Native init app was cleverly evicted, but targetSdkVersion 22 on some phones running 6.0 or higher cannot obtain some permissions. At least what I know is that LeEco cannot escape, and other mobile phones should also have it. Moreover, this is an Android security mechanism, and all apps developed now should try to comply with it. I don’t have much to explain. If you want to know, you can do a search
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Copy the code
The effect
The premise
(Android/app/SRC/Androidmainfest.xml) targetSdkVersion changed to 23 or above, why change???? See aboveCopy the code
start
It doesn’t remind the user to select it again. If the user rejects it, he can apply again. And then the request money will pop up in a dialog box. The following three permissions to do the explanation is actually dead.
The first step
1. The android/app/SRC/AndroidMainfest XML add <! --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/ > <! --> <uses-permission Android :name="android.permission.CAMERA"/ > <! --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Copy the code
The second step
// Add the import {PermissionsAndroid} from RN'react-native'
Copy the code
The third step
1. Permissionsandroid. check(permission) // Permission is String type // Return String type'granted': agreed'denied': refused'never_ask_again': permanent refused to request again next time the user can't see, taking an embarrassing 2. PermissionsAndroid. Request (permission, rationale comment?) // Permission is a String, Rationale comment / / returns an object (3) PermissionsAndroid. RequestMultiple (permissions) / / permissions of type String array / / as an example Remember to add async asynchronous asyncrequestReadPermission() {try {/ / return type string const granted = await PermissionsAndroid. Request ( PermissionsAndroid. PERMISSIONS. WRITE_EXTERNAL_STORAGE, {/ / request after refused to prompt the user for the first time why do you want this permission'title': 'I want read and write access'.'message': 'I can't work without permission. Just say yes.'})if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.show("You have read and write permission.")}else {
this.show("Obtaining read and write permission failed")}} catch (err) {this.show(err.tostring ())}} // verifycheckPermission() {try {/ / return Promise type const granted = PermissionsAndroid. Check (PermissionsAndroid. PERMISSIONS. WRITE_EXTERNAL_STORAGE ) granted.then((data)=>{ this.show("Do I have read and write permission?"+data)}). Catch ((err)=>{this.show(err.toString())})} Catch (err) {this.show(err.tostring ())}} // Request multiple asyncsrequestMultiplePermission() { try { const permissions = [ PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, PermissionsAndroid. PERMISSIONS. CAMERA] / / return to object type const granteds = await PermissionsAndroid.requestMultiple(permissions) var data ="Do you agree with address permissions:"
if (granteds["android.permission.ACCESS_FINE_LOCATION"= = ="granted") {
data = data + "It is \ n"
} else {
data = data + "No \ n"
}
data = data+"Do you agree with camera permissions?"
if (granteds["android.permission.CAMERA"= = ="granted") {
data = data + "It is \ n"
} else {
data = data + "No \ n"
}
data = data+"Do you agree with storage permission?"
if (granteds["android.permission.WRITE_EXTERNAL_STORAGE"= = ="granted") {
data = data + "It is \ n"
} else {
data = data + "No \ n"
}
this.show(data)
} catch (err) {
this.show(err.toString())
}
}
Copy the code
The complete code
import React,{Component} from 'react'
import {
StyleSheet,
View,
Text,
TouchableOpacity,
ToastAndroid,
PermissionsAndroid,
} from 'react-native'
export default class PermissionAndroidView extends Component {
render() {
return( <View style={styles.container}> <TouchableOpacity style={styles.button_view} OnPress = {this. RequestReadPermission. Bind (this)} > < Text style = {styles. Button_text} > application read and write access < / Text > < / TouchableOpacity > <TouchableOpacity style={styles.button_view} onPress={this.requestCarmeraPermission.bind(this)}> <Text Style ={styles.button_text}> Apply for camera permission </Text> </TouchableOpacity> <TouchableOpacity style={styles.button_view} OnPress = {this. RequestLocationPermission. Bind (this)} > < Text style = {styles. Button_text} > apply for access rights to the address < / Text > </TouchableOpacity> <TouchableOpacity style={styles.button_view} onPress={this.checkPermission.bind(this)}> <Text </Text> </TouchableOpacity> <TouchableOpacity style={styles.button_view} OnPress = {this. RequestMultiplePermission. Bind (this)} > < Text style = {styles. Button_text} > a apply for the permission < / Text > </TouchableOpacity> </View>)} show(data) {toastAndroid.show (data, toastAndroid.short)} /* * A dialog box is displayed asking the user for a permission. Returns a Promise, the final value being a Boolean on whether the user agrees to the permission request. * The rationale parameter is optional and is structured as an object containing title and message. * This method negotiates with the system whether to pop up the system's built-in permission request dialog box, * or display the rationale information for the user to explain. * */ asyncrequestReadPermission() {try {/ / return type string const granted = await PermissionsAndroid. Request ( PermissionsAndroid. PERMISSIONS. WRITE_EXTERNAL_STORAGE, {/ / request after refused to prompt the user for the first time why do you want this permission'title': 'I want read and write access'.'message': 'I can't work without permission. Just say yes.'})if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.show("You have read and write permission.")}else {
this.show("Obtaining read and write permission failed")
}
} catch (err) {
this.show(err.toString())
}
}
async requestCarmeraPermission() { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.CAMERA, {// Ask the user why you want this permission after the first request is rejected'title': 'I need camera access.'.'message': 'I can't work without permission. Just say yes.'})if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.show("You have access to the camera.")}else {
this.show("Failed to obtain camera")
}
} catch (err) {
this.show(err.toString())
}
}
async requestLocationPermission() { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {// Ask the user why you want this permission after the first request is rejected'title': 'I need access to address query.'.'message': 'I can't work without permission. Just say yes.'})if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.show("You have obtained access to the address.")}else {
this.show("Failed to get address query")
}
} catch (err) {
this.show(err.toString())
}
}
checkPermission() {try {/ / return Promise type const granted = PermissionsAndroid. Check (PermissionsAndroid. PERMISSIONS. WRITE_EXTERNAL_STORAGE ) granted.then((data)=>{ this.show("Do I have read and write permission?"+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] / / return to object type const granteds = await PermissionsAndroid.requestMultiple(permissions) var data ="Do you agree with address permissions:"
if (granteds["android.permission.ACCESS_FINE_LOCATION"= = ="granted") {
data = data + "It is \ n"
} else {
data = data + "No \ n"
}
data = data+"Do you agree with camera permissions?"
if (granteds["android.permission.CAMERA"= = ="granted") {
data = data + "It is \ n"
} else {
data = data + "No \ n"
}
data = data+"Do you agree with storage permission?"
if (granteds["android.permission.WRITE_EXTERNAL_STORAGE"= = ="granted") {
data = data + "It is \ n"
} else {
data = data + "No \ 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'}})Copy the code
// If I don’t sleep at 12 o ‘clock, I will die suddenly
I don’t have time to explain it, but I can just run it and see how it works. The React Native project is clearly explained
Author: mochixuan links: www.jianshu.com/p/29be198da… The copyright belongs to the author, any form of reprint please contact the author to obtain authorization and indicate the source.