gpt4 book ai didi

Android 6.0 权限对话框不显示

转载 作者:行者123 更新时间:2023-11-29 00:04:17 25 4
gpt4 key购买 nike

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

AndroidManifest.xml

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

构建.gradle

compileSdkVersion 23
buildToolsVersion '23.0.2'

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

Activity

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

当我点击一个项目时,权限对话框不显示, toast 文本是heihei,我不知道为什么,请帮助。

最佳答案

您不能像使用 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_FINE_LOCATION,则权限 ACCESS_COARSE_LOCATION 会被隐式授予,因此您可以忽略它。

关于Android 6.0 权限对话框不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35127763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com