Home » Android » android – ActivityCompat.requestPermissions not working

android – ActivityCompat.requestPermissions not working

Posted by: admin June 15, 2020 Leave a comment

Questions:

Calling ActivityCompat.requestPermissions does not display the UI dialog box.

ActivityCompat.requestPermissions(MainActivity.this, new String[]{"Manifest.permission.READ_SMS"}, REQUEST_CODE_ASK_PERMISSIONS);

However, if I change the minSDKversion to 23 and run

requestPermissions(new String[]{"android.permission.READ_SMS"}, REQUEST_CODE_ASK_PERMISSIONS);

the dialog appears. Why? BTW. to run it on the emulator requires that the emulator will be targeting API 23.

How to&Answers:

Why?

Probably because you have the wrong permission name in the first code snippet. Either use:

Manifest.permission.READ_SMS

or use:

"android.permission.READ_SMS"

Do not use:

"Manifest.permission.READ_SMS"

Answer:

New versions of Android Studio adds the AppCompat library and Android Design Support library dependencies automatically in your build.gradle file on new project creation. If not add the following two lines to the dependencies section of the app’s build.gradle file.

compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'

Answer:

Check that you have already added the requested permission in Android’s manifest file like before Android M, only then you will get expected behaviour.

Add the permission to your manifest:

<uses-permission android:name="android.permission.READ_SMS" />