【如何向android shell用户授予android.permission.RECORD_AUDIO权限()】弱龄寄事外,委怀在琴书。这篇文章主要讲述如何向android shell用户授予android.permission.RECORD_AUDIO权限?相关的知识,希望能为你提供帮助。
我用android NDK构建了一些命令行工具,并在/ data / local / tmp中执行它。现在它提示我“需要android.permission.RECORD_AUDIO”。它通常在AndroidManifest.xml中用于java应用程序,但如何将其授予android shell用户?
源代码如:
sp<
AudioRecord>
rec = new AudioRecord(AUDIO_SOURCE_MIC, 44100, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_MONO);
logcat的:
I/AudioFlinger( 3772): AudioFlinger's thread 0xf0e838c0 ready to run
W/ServiceManager( 3772): Permission failure:android.permission.RECORD_AUDIO from uid=2000 pid=-1
E/( 3772): Request requires android.permission.RECORD_AUDIO
E/AudioFlinger( 3772): openRecord() permission denied: recording not allowed
E/AudioRecord(14132): AudioFlinger could not create record track, status: -1
--------- beginning of crash
...
答案在Lollipop之后,Android平台正在使用实时权限。如果您的应用在Marshmallow和版本之后运行,则应在Activity中授予您的权限。
请求权限之前:
if (Build.VERSION.SDK_INT >
= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
PackageManager.PERMISSION_GRANTED) {
// put your code for Version>
=Marshmallow
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.RECORD_AUDIO)) {
Toast.makeText(this,
"App required access to audio", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO
}, REQUEST_CAMERA_PERMISSION_RESULT);
}} else {
// put your code for Version <
Marshmallow
}
在此之后,请覆盖此方法并输入您的代码:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_AUDIO_PERMISSION_RESULT) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),
"Application will not have audio on record", Toast.LENGTH_SHORT).show();
}
}
}
另一答案
private void requestRecordAudioPermission() {String requiredPermission = Manifest.permission.RECORD_AUDIO;
// If the user previously denied this permission then show a message explaining why
// this permission is needed
if (getActivity().checkCallingOrSelfPermission(requiredPermission) == PackageManager.PERMISSION_GRANTED) {} else {Toast.makeText(getActivity(), "This app needs to record audio through the microphone....", Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{requiredPermission}, 101);
}}@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (requestCode == 101 &
&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// This method is called when thepermissions are given
}}
如果您的设备有marshmallow或高级操作系统,请执行此代码
推荐阅读
- Android Studio外部本机构建预编译头
- 是否有可能改变Android中蓝牙传输的频率(也许通过NDK?)
- 在本地PHP项目中使用xampp启用SSL(https协议)
- 如何在Symfony 3中使用doctrine orm使用多个数据库
- 如何在Windows中使Visual Studio代码窗口透明
- 如何在WinForms C#中使用NAudio将MP3文件转换为WAV
- 如何在Symfony中使用FOSUserBundle和FOSOAuthServerBundle启用电子邮件和用户名登录
- 如何使用Twig获取路径,实际路由和主机域的规范URL
- Web Api(Symfony 2或3和预检的Angular响应具有无效的HTTP状态代码403)