Zvi
Zvi

Reputation: 2424

ActivityCompat.requestPermissions not working

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.

Upvotes: 10

Views: 22606

Answers (3)

CommonsWare
CommonsWare

Reputation: 1007624

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"

Upvotes: 8

Sibin John Mattappallil
Sibin John Mattappallil

Reputation: 1789

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'

Upvotes: 2

Alejandro Casanova
Alejandro Casanova

Reputation: 3681

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" />

Upvotes: 1

Related Questions