mithrandir
mithrandir

Reputation: 191

Android not requiring permissions - Marshmallow 6.0.1 Unity

I'm building a Cardboard app and testing it on a Nexus 6P. The problem I have is that when I install the app, it doesn't ask for any permissions. In my manifest I have this:

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

Then if I want to download some file from the internet in my app, it doesn't work. It doesn't even create the com. folder.

I have to go manually to the app info and check the storage permission.

It's weird because when building the app for GearVR and testing it on a Note 4, it asks for permission (in the Note 4 I use the sd card, in Nexus 6P the internal)

Why is this happening?

Thank you.

Upvotes: 2

Views: 7516

Answers (4)

Ninja Dev
Ninja Dev

Reputation: 836

for a quick fix, you can target android 5 (api level 22 instead of 23) and the permissions will keep using the old behavior.

  • note, if you publish an app targeting android 6 (level 23) api, you can't deprecate the target api level in an update.

https://developer.android.com/training/permissions/requesting.html

Upvotes: 0

Yuri Visser
Yuri Visser

Reputation: 137

For Android 6+ you need to request permissions at runtime, not startup. This is especially the case if you are considering your app for Google Play featuring,

In short, you need to specify the permission in tour manifest, and also include the following in your application tag

 <meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />

Then, request the permission(s) at runtime via Android.

There is a great little plugin for this at https://www.assetstore.unity3d.com/#!/content/62735

Upvotes: 6

Sergey Shustikov
Sergey Shustikov

Reputation: 15821

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. Thats a reason why you didn't have request dialog.


If you need to request permission you need to show request rationale and handle user iteration result.

From docs :

Request permission :

if (ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {
     if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

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

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

Handle User Choosed Option

In ActivityCompat :

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

Upvotes: 2

Owais Ali
Owais Ali

Reputation: 724

Starting with Android 6.0 (Marshmallow) apps no longer get all permissions on install like they used to. You now have to request the permissions at runtime (which the user can decline) and have to be able to handle the permission getting revoked any time after it has been granted.

If you want to use the old behavior for now, I believe you should be able to set your target API level to 22.

Read more here: http://developer.android.com/training/permissions/requesting.html

Upvotes: 0

Related Questions