Android权限对话框无法显示

时间:2016-03-19 17:04:17

标签: android permissions android-6.0-marshmallow

我已经找到了关于这个问题的一些其他问题,但没有一个答案为我解决了。我试图请求精确的位置权限,但没有显示对话框。这就是我正在做的事情:

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);
 }

不显示任何对话框,并且未调用onRequestPermissionsResult

这是清单:

<?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>

以下是gradle文件中的一些花絮:

compileSdkVersion 23
    buildToolsVersion '21.1.2'

    defaultConfig {
        minSdkVersion 12
        targetSdkVersion 23
    }

这是FragmentActivity,不知道这是否重要。我已经卸载了应用程序并运行它,因此应该没有已经接受的权限。这是在运行6.0的Nexus 7上。

1 个答案:

答案 0 :(得分:1)

为什么不试试这个图书馆:https://github.com/tajchert/Nammu。它对我有用。

permissionHelper.verifyPermission

EDIT
我创建了一个封装整个事物的库,使其更容易。它还在实际请求之前显示可自定义的解释对话框。

使用此库:https://github.com/ayz4sci/permissionHelper

用法:
要执行需要Android权限的操作,请致电String []。它接受以下参数:

String [] - 描述所需的每个权限,这将在解释对话框中显示给用户 PermissionCallback - 一系列清单权限
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 } } ); - 您在执行或拒绝许可时执行您要执行的操作 见下面的例子:

{{1}}