请求Android M上的麦克风许可 [英] Request permission for microphone on Android M

查看:2001
本文介绍了请求Android M上的麦克风许可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Android M上使用麦克风.我尝试在清单中设置一个权限组,但无法使其正常工作.这是清单中的内容:

I need to utilize the microphone with Android M. I've tried setting up a permission group in the manifest, and can't get it working properly. Here's what I've got in the manifest:

<permission-group android:name="android.permission-group.MICROPHONE"
    android:label="label"
    android:icon="@mipmap/day_icon"
    android:priority="360"/>

<permission android:name="android.permission.MICROPHONE"
    android:permissionGroup="android.permission-group.MICROPHONE"
    android:protectionLevel="dangerous"
    android:label="label" />

我还尝试通过代码获取访问权限:

I've also tried getting the access through code:

if (ContextCompat.checkSelfPermission(getActivity(),
            Manifest.permission_group.MICROPHONE)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(getActivity(),
                new String[]{Manifest.permission_group.MICROPHONE},
                REQUEST_MICROPHONE);

    }

该警报未显示授予访问权限.

The alert doesn't show to grant access.

我仍然无法使用麦克风.有人知道如何获得麦克风许可吗?

I still don't have access to microphone. Anyone know how get permission for the microphone?

注意:这仅适用于Android M

Note: This is only not working for Android M

推荐答案

要请求麦克风,您应该请求Manifest.permission.RECORD_AUDIO而不是权限组Manifest.permission_group.MICROPHONE.

To request microphone, you should be requesting Manifest.permission.RECORD_AUDIO instead of the permission group Manifest.permission_group.MICROPHONE.

因此,请删除清单中的标签<permission/><permission-group/>,因为它们表示您要创建新权限,而不是使用.

So, remove the tags <permission/> and <permission-group/> in the Manifest because they are to indicate that your want to create new permissions rather than use them.

然后请求许可,只需执行以下操作:

Then to request the permission just do this:

if (ContextCompat.checkSelfPermission(getActivity(),
        Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(getActivity(),
            new String[]{Manifest.permission.RECORD_AUDIO},
            REQUEST_MICROPHONE);

}

这篇关于请求Android M上的麦克风许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆