`targetSdkVersion` 为 30 时,用户不会被提示权限。

huangapple go评论69阅读模式
英文:

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: &#39;com.android.application&#39;
apply plugin: &#39;com.google.gms.google-services&#39;

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

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

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 &lt; permissions.length; i++) {
            String permission = permissions[i];
            boolean isGranted = grantResults[i] &gt;= 0;
            Log.d(&quot;atf&quot;, permission+&quot; isGranted: &quot;+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中的位置更新

> 如在有关如何在运行时请求位置访问权限的指南中所述,您应执行增量位置请求。如果您的应用目标为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)
    新更新为单独逐个请求权限而不是数组格式
    所以尝试并使用以下方式
     在清单文件中
   
      &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
        &lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
        &lt;uses-permission android:name=&quot;android.permission.ACCESS_BACKGROUND_LOCATION&quot;
            android:maxSdkVersion=&quot;30&quot;/&gt;
   
     以及 MainActivty.java 文件中
    
 
    

 

    if (ActivityCompat.checkSelfPermission(AutoStartPermissionActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)!=
                        PackageManager.PERMISSION_GRANTED ){
                    Log.d(&quot;LLLLLLLLLLLLLL&quot;, &quot;checksecondPermission: &quot;+&quot;开始第一个&quot;);
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},100);
                    
            
            }  else {
    checksecondPermission();
                    
                    Log.d(&quot;LLLLLLLLLLLLLL&quot;, &quot;checksecondPermission: &quot;+&quot;结束第一个&quot;);
                }
      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(&quot;LLLLLLLLLLLLLL&quot;, &quot;权限已允许 : &quot;+&quot;结束第一个&quot;);
                    }
                }
    
   

 这段代码在第一次打开应用时完美地工作以获取简单的精确定位权限关闭应用后重新打开应用第二次打开时始终显示使用位置的对话框[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

  &lt;uses-permission android:name=&quot;android.permission.ACCESS_FINE_LOCATION&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_COARSE_LOCATION&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_BACKGROUND_LOCATION&quot;
android:maxSdkVersion=&quot;30&quot;/&gt;
and MainActivty.java
if (ActivityCompat.checkSelfPermission(AutoStartPermissionActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)!=
PackageManager.PERMISSION_GRANTED ){
Log.d(&quot;LLLLLLLLLLLLLL&quot;, &quot;checksecondPermission: &quot;+&quot;start One&quot;);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},100);
}  else {
checksecondPermission();
Log.d(&quot;LLLLLLLLLLLLLL&quot;, &quot;checksecondPermission: &quot;+&quot;End  One&quot;);
}
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(&quot;LLLLLLLLLLLLLL&quot;, &quot;Permission Already Allow : &quot;+&quot;End  One&quot;);
}
}

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

huangapple
  • 本文由 发表于 2020年9月26日 12:06:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64073778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定