8

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?

Note: This is only not working for Android M

coder
  • 10,460
  • 17
  • 72
  • 125

1 Answers1

30

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

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);

}
DeeV
  • 35,865
  • 9
  • 108
  • 95