gpt4 book ai didi

java - targetSdkVersion 30,不提示用户权限

转载 作者:行者123 更新时间:2023-12-05 02:04:47 27 4
gpt4 key购买 nike

从 Android 11 开始,我在 Pixel 3a Xl(更新到 android11 之后)上请求权限时遇到了问题。

所以我有这个应用程序 gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 30
buildToolsVersion '30.0.2'
defaultConfig {
applicationId "nnt.codecexp"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation "com.google.android.gms:play-services-maps:17.0.0"
implementation "com.google.android.gms:play-services-location:17.0.0"
}

以及MainActivity的代码:

public class MainActivity extends AppCompatActivity {

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

@Override
protected void onStart() {
super.onStart();

String[] permissions = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
};

requestPermissions(permissions, 0);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
boolean isGranted = grantResults[i] >= 0;
Log.d("atf", permission+" isGranted: "+isGranted);
}
}
}

我通过 Android Studio 在我的设备上全新安装了该应用程序,日志显示权限自动被拒绝,而没有收到任何授予权限的提示。 (提示窗口未显示)。我知道在 android 11 上,如果用户拒绝权限 2 次,系统将跳过以下权限请求的提示对话框,它们将被自动拒绝。但事实并非如此,问题发生在全新安装应用程序后。 (从 Android Studio 启动时,该应用程序不在设备上)

但是:如果我将 gradle 文件“targetSdkVersion 30”更改为“targetSdkVersion 29”正如预期的那样,一切正常。

谁能告诉我为什么会这样,并且让“targetSdkVersion 29”成为解决这个问题的好方法?

我做错了什么吗?或者只是谷歌 [审查] [审查] [审查]。

最佳答案

根据 Location updates in Android 11 :

As described in the guide on how to request location access at runtime, you should perform incremental location requests. If your app targets Android 11 or higher, the system enforces this best practice. If you request a foreground location permission and the background location permission at the same time, the system ignores the request and doesn't grant your app either permission.

您在请求位置权限的同时请求后台位置。您需要先请求位置权限,然后单独请求ACCESS_BACKGROUND_LOCATION权限。

关于java - targetSdkVersion 30,不提示用户权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64073778/

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