英文:
targetSdkVersion 30, user is not prompted for permission
问题
从Android 11开始,我在Pixel 3a Xl上(升级到Android 11后)请求权限时遇到了问题。
所以我有这个应用的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”是解决这个问题的好方法吗?
我做错了什么吗?还是这只是谷歌的[censored] [censored] [censored]。
英文:
Starting from Android 11 I've been having troubles with requesting permissions on Pixel 3a Xl (after update to android11).
So I have this app gradle file:
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"
}
and the code of 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);
}
}
}
I do a fresh install of the app on my device from Android Studio and the logs show me that permissions where automatically denied without getting any prompt for granting permissions. (The prompt window is not shown). I understand that on android 11 if the user denies permissions like 2 times, the system will skip the prompt dialog on following permissions request and they will be automatically denied. But this is not the case, the issue happens right after a fresh install of the App. (the app is not present on the device when launched from Android Studio)
However:
if I change in the gradle file "targetSdkVersion 30" to "targetSdkVersion 29"
everything works properly, as expected.
Can anyone tell me why is this happening and is keeping "targetSdkVersion 29" a good solution for this problem?
Am I doing something wrong? or is that just Google [censored] [censored] [censored].
答案1
得分: 3
> 如在有关如何在运行时请求位置访问权限的指南中所述,您应执行增量位置请求。如果您的应用目标为Android 11或更高版本,则系统会执行此最佳实践。如果您同时请求前台位置权限和后台位置权限,则系统将忽略该请求并且不会授予应用任何权限。
您正在同时请求后台位置权限和位置权限。您需要首先请求位置权限,然后单独请求ACCESS_BACKGROUND_LOCATION
权限。
英文:
As per the 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.
You're requesting background location at the same time as you are requesting the location permission. You need to first request the location permissions and then separately request the ACCESS_BACKGROUND_LOCATION
permission.
答案2
得分: 0
我解决了这类问题 Android 11 (sdk 30)
新更新为单独逐个请求权限,而不是数组格式
所以尝试并使用以下方式
在清单文件中
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
android:maxSdkVersion="30"/>
以及 MainActivty.java 文件中
if (ActivityCompat.checkSelfPermission(AutoStartPermissionActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)!=
PackageManager.PERMISSION_GRANTED ){
Log.d("LLLLLLLLLLLLLL", "checksecondPermission: "+"开始第一个");
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},100);
} else {
checksecondPermission();
Log.d("LLLLLLLLLLLLLL", "checksecondPermission: "+"结束第一个");
}
private void checksecondPermission() {
if (ActivityCompat.checkSelfPermission(AutoStartPermissionActivity.this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)!=
PackageManager.PERMISSION_GRANTED ){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},100);
} else {
Log.d("LLLLLLLLLLLLLL", "权限已允许 : "+"结束第一个");
}
}
这段代码在第一次打开应用时完美地工作,以获取简单的精确定位权限,关闭应用后重新打开应用,第二次打开时始终显示使用位置的对话框。[1]
[1]: https://i.stack.imgur.com/UOzSi.jpg
英文:
I resolve this types of issue Android 11 (sdk 30)
new Update to Permission Request One by One not a array format
So try and use this one
In manifest
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
android:maxSdkVersion="30"/>
and MainActivty.java
if (ActivityCompat.checkSelfPermission(AutoStartPermissionActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)!=
PackageManager.PERMISSION_GRANTED ){
Log.d("LLLLLLLLLLLLLL", "checksecondPermission: "+"start One");
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},100);
} else {
checksecondPermission();
Log.d("LLLLLLLLLLLLLL", "checksecondPermission: "+"End One");
}
private void checksecondPermission() {
if (ActivityCompat.checkSelfPermission(AutoStartPermissionActivity.this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)!=
PackageManager.PERMISSION_GRANTED ){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION},100);
} else {
Log.d("LLLLLLLLLLLLLL", "Permission Already Allow : "+"End One");
}
}
this code work perfectly first time open app to give simple fine location permission and close app reopen app so second time show all time using location dialog .1
1: https://i.stack.imgur.com/UOzSi.jpg
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论