简体   繁体   中英

React Native check if I have permission

I have written an app which records audio and do real time analysis. For this I must first request permission to use the microphone so I have made a separate button for this but I don't like the solution. The button runs this function:

 const requestMicrophone = async () => { //replace your function with this code. if (Platform.OS === 'android') { try { const granted = await PermissionsAndroid.request( PermissionsAndroid.PERMISSIONS.RECORD_AUDIO, { title: 'Permissions for record audio', message: 'Give permission to your device to record audio', buttonPositive: 'ok', }, ); if (granted === PermissionsAndroid.RESULTS.GRANTED) { console.log('permission granted'); } else { console.log('permission denied'); return; } } catch (err) { console.warn(err); return; } } };

Now I want to check if I have permission to use the microphone. If so render the recording component. I tried this:

 if (PermissionsAndroid.PERMISSIONS.RECORD_AUDIO) { rec = <Recorder/>; } else { rec = <Text>Ask for microphone permission first!</Text>; }

However this does not check if I have permission to use the microphone. What I get from

 console.log(PermissionsAndroid.PERMISSIONS.RECORD_AUDIO);

is just

[Wed Nov 18 2020 18:59:44.878]  LOG      android.permission.RECORD_AUDIO

I just cannot find anywhere how to check if I do have permission to use the microphone or not?

I've found the answer after much googling around:

export const checkMicrophone = async () => {
   const result = await PermissionsAndroid.check(
      PermissionsAndroid.PERMISSIONS.RECORD_AUDIO,
   );
   return result;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM