2

I have found a few other questions about this issue but none of the answers have solved it for me. I'm trying to request the fine location permission, but there is no dialog shown. Here's what I'm doing:

int permissionCheck = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);

if(permissionCheck == PackageManager.PERMISSION_GRANTED) {
    Log.d("Location","Location permission already granted"); // Not logged
    // Do other stuff
}
else {
    Log.d("Location", "Requesting location permission"); // This is logged

    ActivityCompat.requestPermissions(this, 
        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE_ENABLE_LOCATION);
 }

No dialog appears, and onRequestPermissionsResult is not called.

This is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.company.common" android:installLocation="auto">

    <!-- Normal permissions -->
    <uses-permission android:name="android.permission.ACCESS_GPS" android:required="false"/>
    <uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" android:required="false" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="com.android.vending.billing.IN_APP_NOTIFY" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />

    <!-- Dangerous permissions -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" android:required="false" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" android:required="false" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>

    <uses-feature
        android:name="android.hardware.telephony"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true" >
    </supports-screens>

    <application
        android:name="com.company.common.App"
        android:allowBackup="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >

        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />

        <meta-data android:name="AA_DB_NAME" android:value="propertyforce.db" />
        <meta-data android:name="AA_DB_VERSION" android:value="11" />
        <meta-data android:name="AA_SERIALIZERS"
            android:value="com.company.common.utils.db.JSONObjectSerializer,
            com.company.common.utils.db.JSONArraySerializer,
            com.company.common.utils.db.AddressSerializer" />

        <meta-data
            android:name="AA_MODELS"
            android:value="com.company.common.utils.db.Model" />

        <activity
            android:name="com.company.common.Name"
            android:configChanges="orientation|keyboardHidden"
            android:label="@string/app_label"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustPan" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activities.AnActivity"/>
        <!-- Other activities -->

        <service
            android:name="com.company.common.utils.services.LogDeleter"
            android:icon="@drawable/icon"
            android:label="Log Deleter" >
        </service>
    </application>

</manifest>

Here are some tidbits from the gradle file:

compileSdkVersion 23
    buildToolsVersion '21.1.2'

    defaultConfig {
        minSdkVersion 12
        targetSdkVersion 23
    }

This is in a FragmentActivity, don't know if that matters. I have uninstalled the app and run it so there should be no permissions already accepted. This is on a Nexus 7 running 6.0.

nasch
  • 5,330
  • 6
  • 31
  • 52
  • What is your `targetSdkVersion`? Also, note that there is no `android:required` attribute on ``. – CommonsWare Mar 19 '16 at 17:07
  • Thanks, I added the target SDK from the build file (23). I tried removing the `android:required` but it had no effect. – nasch Mar 19 '16 at 17:42
  • Can you post your `AndroidManifest.xml`? – Mattia Maestrini Mar 20 '16 at 21:06
  • I added the manifest, minus some uninteresting stuff and anonymized. – nasch Mar 21 '16 at 14:16
  • I tried the camera permission with the same result, so it doesn't seem to be anything specific to the location permission. – nasch Mar 23 '16 at 14:19
  • I have a similar problem. It stops displaying the dialog whenever I import a certain library. My question is here: http://stackoverflow.com/questions/37009826/adding-the-layer-library-dependency-in-android-suppresses-requestpermissions-dia – Rock Lee May 03 '16 at 17:20
  • Two ideas: (1) Try to add ``. (2) Use `requestPermissions(String[], int)` instead of AppCompat method. Maybee it helps... – Timo Bähr May 13 '16 at 10:49
  • @TimoBähr Tried both of those with no effect – nasch Jun 03 '16 at 18:57

1 Answers1

1

Why not try this library: https://github.com/tajchert/Nammu. It worked for me.

Nammu.askForPermission(activity, String[] permissions, new PermissionCallback() {
    @Override
    public void permissionGranted() {}

    @Override
    public void permissionRefused() {}
});

EDIT
I created a library that encapsulates the whole thing and makes it much more easier. It also shows a customisable explanation dialog before the actual request.

Use this library: https://github.com/ayz4sci/permissionHelper

Usage:
To perform an action that requires Android permission, call permissionHelper.verifyPermission. It accepts the following parameters:

String [] - description of each permission required, this will be displayed to the user in the explanation dialog.
String [] - an array of Manifest permissions
PermissionCallback - you put the actions you want to perform when permission is granted or rejected.
See example below:

permissionHelper.verifyPermission(
    new String[]{"dial this number", "take picture"},
    new String[]{Manifest.permission.CALL_PHONE, Manifest.permission.CAMERA}, 
    new PermissionCallback() {
        @Override
        public void permissionGranted() {
            //action to perform when permission granteed
        }

        @Override
        public void permissionRefused() {
            //action to perform when permission refused
        }
    }
);
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
ayz4sci
  • 2,220
  • 1
  • 16
  • 17
  • Why do I need a library? Isn't this supposed to work out of the box? – nasch May 04 '16 at 18:26
  • Finally got back to this issue, and this doesn't solve it. When I request a permission through the Nammu library, nothing happens, which is not surprising. I think Nammu just wraps the permission API, so whatever underlying problem prevents the dialog from displaying isn't solved. – nasch Jun 03 '16 at 18:42
  • Give me a day or two. I'm working on a library that encapsulate all the work and makes it easier for you. – ayz4sci Jun 06 '16 at 15:46
  • Sounds fantastic if it works but I fear it will just encapsulate the problem just like Nammu did. As far as I can tell my issue is not that this is too complicated and I can't figure out how to do it right, it's that I'm doing everything right and nothing happens (would love to be proven wrong on that though). In that case making it easier will just make it easier to get to the same problem. Looking forward to seeing your work though. – nasch Jun 07 '16 at 16:53
  • I've implemented it and edited the answer. You check above or the github page for more info. – ayz4sci Jun 15 '16 at 15:43
  • And this is a wrapper that passes the requests through to the android API? – nasch Jun 15 '16 at 16:42
  • Yeah and also handles the callback and any explanation instruction needed. – ayz4sci Jun 16 '16 at 07:15
  • The both links are "dead" X_X – naXa stands with Ukraine Oct 20 '16 at 14:30
  • Yes. I guess it was a Github issue... cause I saw 404 page. – naXa stands with Ukraine Oct 20 '16 at 14:38