10

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.

cuihtlauac
  • 1,808
  • 2
  • 20
  • 39
Zvi
  • 2,354
  • 2
  • 26
  • 37

3 Answers3

8

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"
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Tried that already - giving me "cannot resolve symbol READ_SMS". That is why I tried the string version as posted above. – Zvi Sep 20 '15 at 23:48
  • @Zvi: "giving me "cannot resolve symbol READ_SMS"" -- did you add the `import` for `android.Manifest`? `android.Manifest.permission.READ_SMS` has been around [since API Level 1](http://developer.android.com/reference/android/Manifest.permission.html#READ_SMS). And, if you read [the documentation for `READ_SMS`](http://developer.android.com/reference/android/Manifest.permission.html#READ_SMS), you will see that the string value of the `android.Manifest.permission.READ_SMS` constant is `"android.permission.READ_SMS"`. – CommonsWare Sep 20 '15 at 23:52
  • You were right, I did not have that import. I am so used to the fact that Android Studio is prompting for the imports that I failed to check if it imported all the libraries needed in that activity. Maybe it is a bug in AS because it did not prompt for the import of that library, and when I googled for READ_SMS I did not find any solution talking about the import. Thx. – Zvi Sep 21 '15 at 21:14
  • I have this problem today, and I use the Manifest.permission.X, have the right stuff in AndroidManifest.xml. I call `ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 1);` but nothing appears on-screen... – Ted Dec 05 '22 at 20:47
  • @Ted: For that one, you need to have `targetSdk` set for Android 13, see https://developer.android.com/develop/ui/views/notifications/notification-permission#declare – CommonsWare Dec 05 '22 at 21:01
  • Thanks, I do have that actually: defaultConfig { applicationId "test.myapp" minSdkVersion 29 targetSdkVersion 33 versionCode 60 versionName "1.60" } I was consideriong if running this in onCreate was the problem, but doesnt seem to be. Maybe another So question is warranted =) – Ted Dec 05 '22 at 21:02
  • So, here goes =) https://stackoverflow.com/questions/74694632/android-activitycompat-requestpermissions-does-not-show-a-popup-android-13-ta – Ted Dec 05 '22 at 21:32
2

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'
Sibin John Mattappallil
  • 1,739
  • 4
  • 23
  • 37
1

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" />
Alejandro Casanova
  • 3,633
  • 4
  • 31
  • 46