Android运行时权限对话框未显示

时间:2016-11-27 13:32:43

标签: java android permissions

我正在尝试在运行时获取ACCESS_FINE_LOCATION的权限(来自运行Android版本高于6.0的用户)。这在我的主应用程序中失败了,所以我尝试制作一个测试应用程序,其唯一目的是向用户请求位置权限。

我遇到的问题是没有向用户显示权限对话框 - 从logcat我可以看到' onRequestPermissionsResult'方法正在直接运行(不要求用户接受权限),并显示未授予权限。

在查看有关此主题的其他问题后,我仔细检查了“使用 - 权限”#39; line在我的Android Manifest XML文件中,但无法找到任何其他解决方案。

这是我的代码:

package com.user.testapplication;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_LOCATION_ID = 1;
private static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    checkLocationPermissions();
}

private void checkLocationPermissions(){
    if (ContextCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            Log.i(TAG, "Device version above 6.0 - Requesting location permissions.");
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    REQUEST_LOCATION_ID);
        }

    }
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_LOCATION_ID: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.i(TAG, "Permissions granted successfully!");

            } else {
                Log.i(TAG, "Permissions were not granted.");
            }
            return;
        }
    }//end switch
}

}//end class

我的宣言:

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

<uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION"/>
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

来自logcat的相关行:

11-27 13:27:47.752 31839-31839/com.user.testapplication I/MainActivity: Device version above 6.0 - Requesting location permissions.
11-27 13:27:47.881 31839-31839/com.user.testapplication I/MainActivity: Permissions were not granted.

非常感谢任何帮助 - 我想我可能只是遗漏了一些东西。

3 个答案:

答案 0 :(得分:1)

在onStart()方法中使用它,它将解决您的问题。

 //This checks for the permission
    if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)){


            // You can show your dialog message here but instead I am 
            // showing the grant permission dialog box
            ActivityCompat.requestPermissions(this, new String[] {
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION },
                    10);



        }
        else{

            //Requesting permission
            ActivityCompat.requestPermissions(this, new String[] {
                            Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION },
                    10);
        }
    }

答案 1 :(得分:0)

Dialog框中没有显示AndroidManifest框的原因是错误,您可以看到权限拼写为android-permisssion而不是android-permission

答案 2 :(得分:0)

我也遇到了同样的问题,我的问题是我在清单中写了以下行

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

代替

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