Android 6.0权限对话框未显示

时间:2016-02-01 10:08:07

标签: android android-6.0-marshmallow runtime-permissions

我想在Android 6.0中尝试请求权限,但我遇到了麻烦。

的AndroidManifest.xml

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

的build.gradle

compileSdkVersion 23
buildToolsVersion '23.0.2'

defaultConfig {
    applicationId "com.zhangkaiyue.jkdemo"
    minSdkVersion 15
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}

活动

    mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (Build.VERSION.SDK_INT >= 23) {
                int checkPermission = ContextCompat.checkSelfPermission(UsingCustomLayoutActivity.this, Manifest.permission_group.LOCATION);
                if (checkPermission != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(UsingCustomLayoutActivity.this, new String[]{Manifest.permission_group.LOCATION}, 111);
                }
            } else {
                Toast.makeText(UsingCustomLayoutActivity.this, "haha", Toast.LENGTH_SHORT).show();
            }
        }
    });

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == 111){
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(UsingCustomLayoutActivity.this, "hehe", Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(UsingCustomLayoutActivity.this, "heihei", Toast.LENGTH_SHORT).show();
        }
    }
}

当我点击一个项目时,权限对话框未显示,而吐司文字为heihei,我不知道为什么,请帮忙。

2 个答案:

答案 0 :(得分:1)

您无法像使用Manifest.permission_group.LOCATION一样申请整个权限组的权限。

您必须申请特定权限,即Manifest.permission.ACCESS_FINE_LOCATION

int checkPermission = ContextCompat.checkSelfPermission(UsingCustomLayoutActivity.this, 
        Manifest.permission.ACCESS_FINE_LOCATION);

if (checkPermission != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(UsingCustomLayoutActivity.this, 
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 111);
}

另请注意,如果您已经拥有ACCESS_COARSE_LOCATION,则会隐式授予权限ACCESS_FINE_LOCATION,因此您可以省略它。

答案 1 :(得分:1)

请仔细检查<application />标记<{1}} - 标记 <{1}}标记。即使是艰难的我也多次阅读相同的提示,我花了大约2个小时才意识到我犯了同样的错误......