80

I'm currently trying to adapt my application to the new permissions model of Android M.

I'm collecting all the permissions I require, then run

Log.i("Permissions", "Requesting permissions: " + permissions);
requestPermissions(requiredPermissions.toArray(new String[requiredPermissions.size()]), requestCodeForPermissions);

requiredPermissions holds the permissions I need like android.permission.WRITE_EXTERNAL_STORAGE.

That routine is definitely executed as I have the Log line in the logcat:

08-07 12:52:46.469: I/Permissions(1674): Requesting permissions: android.permission.RECEIVE_BOOT_COMPLETED; android.permission.WRITE_EXTERNAL_STORAGE

But the permissions dialog never shows, let alone is onRequestPermissionsResult() called.

What am I doing wrong? Based on some tutorials I found I'm not missing anything. I only have the emulator for testing, no physical device. This is the about screen from settings: Image

It might be worth mentioning something else: If I try to open the overview of installed apps from the home screen I only get launcher3 has exited. I'm not sure if that might be related.

Does anybody have an idea why it's not showing?

cuihtlauac
  • 1,808
  • 2
  • 20
  • 39
Jens
  • 1,157
  • 1
  • 8
  • 17
  • 4
    What version of the M Developer Preview are you using? In v1, `WRITE_EXTERNAL_STORAGE` was not `dangerous` and did not need to be requested manually. That changed in v2, where it works like other `dangerous` permissions like `CAMERA`. Note that `RECEIVE_BOOT_COMPLETED` is not a `dangerous` permission; you do not have to request it at runtime. – CommonsWare Aug 07 '15 at 13:08
  • I have v2. For the sake of testing I added record audio which is now requested: 08-07 13:49:31.647: I/Permissions(1540): Requesting permissions: android.permission.RECEIVE_BOOT_COMPLETED; android.permission.WRITE_EXTERNAL_STORAGE; android.permission.RECORD_AUDIO This is from my manifest: And this is from the project.properties file: target=android-MNC targetSdkVersion=MNC compileSdkVersion=android-MNC Am I missing anything that might be obvious for others? – Jens Aug 07 '15 at 13:50
  • 2
    Do you have the `` elements in your manifest? The new runtime permissions model does not eliminate the need for `` elements. It just adds additional requirements for requesting permissions at runtime for select permission groups. – CommonsWare Aug 07 '15 at 13:52
  • Yes, my manifest still contains all the old uses-permission elements. – Jens Aug 07 '15 at 13:54
  • Well, [this sample app of mine](https://github.com/commonsguy/cw-omnibus/tree/master/Permissions/PermissionMonger) seems to work fine on MNC v2. Compare and contrast what you have with what I have. – CommonsWare Aug 07 '15 at 14:04
  • Thanks for that. Well, I don't currently use Android Studio, but still Eclipse. Most tutorials about this mention the build.gradle file for some lines. Could it be Android Studio is a requirement for the permission model? The actual source code seems identical. – Jens Aug 07 '15 at 23:54
  • "Could it be Android Studio is a requirement for the permission model?" -- it's conceivable, at least for the preview. Eclipse might work better once M ships and we have a regular API level number to work with. That being said, since Google is abandoning support for Eclipse at the end of the year, I really encourage you to have a plan for issues like this one, as there will come a time when you literally cannot build what you want using Eclipse+ADT. With luck, Andmore will be able to fill the gap by then. – CommonsWare Aug 08 '15 at 10:21
  • in my case i had set a maxsdk for the WRITE permission, because i wasn't ready to deal with permissions(earlier), so i just targeted API 21. so all i had to do was remove the MAXSDK restriction and it worked. – Peterstev Uremgba Oct 22 '17 at 16:16

29 Answers29

124

I experienced the same issue but later I realized I forgot to add the permission to the manifest file. After adding the uses-permission tag, the system showed the dialog. Maybe helps someone.

Tamás Cseh
  • 3,050
  • 1
  • 20
  • 30
32

The original answer helped me.

I fixed by adding tools:remove="android:maxSdkVersion" like this:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion"/>
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Khang .NT
  • 1,504
  • 6
  • 24
  • 46
14

I experienced the same issue because I was using the negative value as a REQUEST_CODE.

requestPermissions(new String[]{android.Manifest.permission.CAMERA}, -1)

After using positive value, the system showed the dialog. Hope it helps someone.

shanraisshan
  • 3,521
  • 2
  • 21
  • 44
10

Based on the comment from Hilal (thanks a lot!): In my case my app is indeed using tabhost and the permissions were requested from an Activity inside the tabhost. After starting a separate activity that requests the permissions it is working.

Jens
  • 1,157
  • 1
  • 8
  • 17
9

add

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

in AndroidManifest.xml

Note : The Permission which you want to get. Eg: android.permission.ACCESS_FINE_LOCATION etc.

Vinil Chandran
  • 1,563
  • 1
  • 19
  • 33
9

I just had the same problem. My issue was that I wrote the permission at the wrong place in the manifest. Make sure the uses permission is outside of application:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.samples.vision.face.photo"
    android:installLocation="auto"
    android:versionCode="1"
    android:versionName="1" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="21" />

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


    <application
        android:hardwareAccelerated="true"
        android:label="FacePhotoDemo"
        android:allowBackup="true"
        android:icon="@drawable/icon">
Loïs Talagrand
  • 810
  • 2
  • 13
  • 32
2

I have also come across a situation where the permission dialog doesn't appear or the application crashes when using the <uses-permission-sdk23> element, however the cause appears to be a system bug on current 6.0 devices:

https://code.google.com/p/android/issues/detail?id=189841

Crash exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to get length of null array
TheIT
  • 11,919
  • 4
  • 64
  • 56
2

I have the same issue and the problem is solved after adding the shouldShowRequestPermissionRationale like this:

if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {

} else {
    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

}
Hai nguyen thanh
  • 718
  • 7
  • 17
2

To add to @shanraisshan's answer, the REQUEST_CODE actually has to be greater than 0, not just non-negative.

joakim
  • 3,533
  • 2
  • 23
  • 28
2

In our code, it was a simple spelling mistake.

We had:

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

It should be:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Pang
  • 9,564
  • 146
  • 81
  • 122
RJ.
  • 141
  • 1
  • 4
2

I was doing two calls to requestPermissions(), one right after the other. This error message appeared in Logcat (note the misspelling on "request"):

Can reqeust only one set of permissions at a time

requestPermissions() is actually designed to take multiple requests at once; that's what the String array is for.

paperduck
  • 1,175
  • 1
  • 16
  • 26
2

In my case, the permission I was requesting (WRITE_SETTINGS) was more special and required Settings Activity to launch. So dialog was not showing up.

I had to get its permission using the following code:

Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
startActivityForResult(intent, CODE_WRITE_SETTINGS_PERMISSION);
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
  • `WRITE_SETTINGS` is treated as a _special permission_ and requires special handling: https://developer.android.com/guide/topics/permissions/overview#special_permissions – ccpizza Feb 03 '19 at 22:31
2

Yet another cause for not getting the permission dialog to show when requesting a dangerous permission...

I had to do Build -> Clean Project and then Build -> Rebuild Project. I guess Android Studio didn't pick up on the changes I made in the manifest.

Magnus
  • 17,157
  • 19
  • 104
  • 189
2

I had a similar issue caused by the wrong case of the permission constant in the manifest, I was using read_contacts in lower case:

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

After changing read_contacts to uppercase it started working as expected

<uses-permission android:name="android.permission.READ_CONTACTS" />
Roberto
  • 4,524
  • 1
  • 38
  • 30
1

Permissions are organised into categories so non-critical ones are granted without the dialog being shown.

I found this the hard way with internet permission, if you're having this issue then changing to a critical permission such as read_contacts will allow you to test your flow and reveal whether the issue is the permission being non-critical or something else.

Normal protection permissions are listed here

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
1

In the manifest, I changed

 <uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="22" />

to

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

Maybe that helps.

mogile_oli
  • 2,148
  • 2
  • 21
  • 21
1

Add permission in the manifest file as well.

Phani varma
  • 475
  • 5
  • 7
1

If anyone of you guys has an application that changes/modifies WRITE_SETTINGS and are facing this issue. Check out CommonsWare's Answer to WRITE_SETTINGS Ambiguity

I was facing this issue for 2 weeks and later realised that this issue was due to the fact that requestPermissions doesn't work for requesting WRITE_SETTINGS permission. Hope this helps :D

Irfan S
  • 267
  • 5
  • 14
1

In my case I had requested "ACCESS_COARSE_LOCATION" in my manifest file and then request for "ACCESS_FINE_LOCATION" permission in code that's why the Permission Dialog was not opening.

Akash Bisariya
  • 3,855
  • 2
  • 30
  • 42
0

After searching a while it appears it is required to set the value compileSdkVersion to "android-MNC" or as of today to 23. That requires the Gradle build system which then seems to require Android Studio. At least I couldn't find a single manual about how to set it outside the gradle files.

Jens
  • 1,157
  • 1
  • 8
  • 17
  • I have the same issue and haven't managed to solve it yet. Reading your answer does not really help me solve it? Could you please clarify a bit more? My compileSdkVersion was and still is 23 and I still get this error. – SudoPlz Nov 19 '15 at 14:16
  • me too. Your answer worth nothing, because below 23 there was no Runtime Permissions. – Evgeniy Mishustin Jan 08 '16 at 14:26
  • Why is it worth nothing? I merely said it doesn't seem to work to achieve this if one is using Eclipse. – Jens Jan 09 '16 at 23:21
0

I did all the things said in above answers but still dialog was not showing and then I changed targetSdkVersion to 23 in gradle and it appeared . Hope this helps someone

Naveen Rao
  • 712
  • 6
  • 10
0
@Override
    public final void validateRequestPermissionsRequestCode(int requestCode) {
       // We use 16 bits of the request code to encode the fragment id when
       // requesting permissions from a fragment. Hence, requestPermissions()
       // should validate the code against that but we cannot override it as we
       // can not then call super and also the ActivityCompat would call back to
       // this override. To handle this we use dependency inversion where we are
       // the validator of request codes when requesting permissions in
       // ActivityCompat.
        if (!mRequestedPermissionsFromFragment
                && requestCode != -1 && (requestCode & 0xffff0000) != 0) {
            throw new IllegalArgumentException("Can only use lower 16 bits for requestCode");
        }
    }
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
0

Had the same issue. Later I realized that we have to declare each and every permission in manifest (even if one is a subclass of another). In my case I declared

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

in my manifest and was trying to access user's coarse location.

ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)

Fixed the problem by adding coarse permission as well in manifest.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
shahrukhamd
  • 259
  • 6
  • 6
0

Set your targetSdkVersion to 22.

Iman Marashi
  • 5,593
  • 38
  • 51
0

had the same (i guess) problem and the solution was removing

import <app package>.Manifest;

autoimported by Android Studio at the top of the file and substitute it with

import android.Manifest; 

and started working

0

In my case the solution is the string itself

android.permission.READ_CONTACTS

I did Manifest.permission.READ_CONTACTS which caused silence error (Noting show on the screen).

Make sure that this is correct

Kadir Erturk
  • 583
  • 6
  • 8
0

My Android targetSDK version is 28.

I don't see any pop ups shown to user requesting for permission which are listed in android Manifest.xml as . During both installation, installing .apk using USB and installing app from google play store. So I added below code in my activity it will ask user permission during runtime

ActivityCompat.requestPermissions(MyActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);

Smitha
  • 555
  • 4
  • 7
0

I was requesting permission through code, but had missed adding the <uses-permission ..> tag in manifest !

AndroidGuy
  • 1,270
  • 4
  • 15
  • 32
-1

Change the final constant value to 1.

private static final int REQUEST_PERMISSION_WRITE = 1;

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_PERMISSION_WRITE);
anto004
  • 287
  • 2
  • 6