简体   繁体   English

React Native 检查我是否有权限

[英]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;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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