Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request permission for microphone on 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?

Note: This is only not working for Android M

like image 566
coder Avatar asked Mar 28 '16 20:03

coder


People also ask

How do I get microphone permission on Android?

On your phone, open the Settings app. Tap Privacy. Turn off Camera access or Microphone access.

How do I request microphone permission?

Here's how: Select Start > Settings > Privacy > Microphone . In Allow access to the microphone on this device, select Change and make sure Microphone access for this device is turned on.

How do I request permission in Android 11?

Starting in Android 11, whenever your app requests a permission related to location, microphone, or camera, the user-facing permissions dialog contains an option called Only this time. If the user selects this option in the dialog, your app is granted a temporary one-time permission.


1 Answers

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

}
like image 199
DeeV Avatar answered Dec 21 '22 16:12

DeeV