targetSdkVersion 30, user is not prompted for permission

Viewed 4826

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].

2 Answers

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.

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

Related