2

The first few times I tried the code out, it prompted me each time requesting for the permissions but after a while, it would just go straight to my manual input page.

Also, I'm not sure if it would be relevant but my targetSDKVersion is 30.

Please and thank you for the help :)

I included these permissions in my AndroidManifext.xml

<uses-permission android:name="android.permission.INTERNET" />
    <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" /> 

The general flow of my code is that it'll check the permissions like so

private boolean checkPermissions() {
        return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED;
    }

And then, if the permissions are not granted, it should request the user for their location calling requestPermissions()

    private void requestPermissions() {
        Log.d(TAG, "requestPermissions");
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_BACKGROUND_LOCATION}, PERMISSION_ID);
    }

And after that:

    // If everything is alright then
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PERMISSION_ID) { 
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getCurrentLocation();
            } else {
                //if user denies permission to use GPS services 
                //update UI accordingly for manual input
        }
    }

But it doesn't request, and I don't know why. I've read the documentation here https://developer.android.com/training/location/permissions, but I just don't get it :\

bababoo
  • 21
  • 2
  • 1
    You've missed red warning box: `Caution: If your app targets Android 11 (API level 30) or higher, the system enforces this [incremental requests] 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.` – Pawel Aug 15 '21 at 19:20
  • Hi Pawel, thanks for the reply :) I removed the permission request for Background Location and it worked! – bababoo Aug 22 '21 at 11:25

2 Answers2

2

I've been searching for this for a long time. You first have to ask for basic location permissions, when those granted, then ask for background location permission. It will take the user to phone settings page to allow all the time.

private void LocationPermission() {

    ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_CODE);


}


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

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == LOCATION_CODE) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Toast.makeText(MainActivity.this, R.string.permission_granted, Toast.LENGTH_SHORT).show();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, LOCATION_CODE2);
            }else{
                getLocation();
            }

        } else {

            Toast.makeText(this, "Location access is required!", Toast.LENGTH_SHORT).show();

        }
    }
    if (requestCode == LOCATION_CODE2) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //Toast.makeText(MainActivity.this, R.string.permission_granted, Toast.LENGTH_SHORT).show();
            Toast.makeText(this, "Location access granted!", Toast.LENGTH_SHORT).show();
            getLocation();


        } else {

            Toast.makeText(this, "Location access is required!", Toast.LENGTH_SHORT).show();

        }
    }
}
A Anand
  • 51
  • 5
0

Helloo~

I've removed the permission requests for Manifest.permission.ACCESS_BACKGROUND_LOCATION and it workds now :O!!

Thanks to Pawel for pointing it out :D

bababoo
  • 21
  • 2