Background Location Permission in Android 11 and above
June 23, 2021 by Mohit Singh
Request background location permission in all android versions using Java
With lastest android version Android 11, some security is added for location. Now to access background location, users have to manually “Allow all the time” in settings.
For using foreground and background location in lower versions of Android 11, we can take permission ACCESS_FINE_LOCATION, this permission will be considered for both foreground and background but with Android 11 and above you have to take one more permission ACCESS_BACKGROUND_LOCATION separately.
Ok now we will see how we can achieve background location permission for Android 11.
Step-1 :
Add these permissions in your mainfest file:
<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" />
Step-2 :
I added a simple button in activity_main.xml with id “location_permission”, on click of the button call checkPermission() function:
private void checkPermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// Fine Location permission is granted
// Check if current android version >= 11, if >= 11 check for Background Location permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// Background Location Permission is granted so do your work here
} else {
// Ask for Background Location Permission
askPermissionForBackgroundUsage();
}
}
} else {
// Fine Location Permission is not granted so ask for permission
askForLocationPermission();
}
}
private void askForLocationPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
new AlertDialog.Builder(this)
.setTitle("Permission Needed!")
.setMessage("Location Permission Needed!")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Permission is denied by the user
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_PERMISSION_CODE);
}
}
private void askPermissionForBackgroundUsage() {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
new AlertDialog.Builder(this)
.setTitle("Permission Needed!")
.setMessage("Background Location Permission Needed!, tap \"Allow all time in the next screen\"")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, BACKGROUND_LOCATION_PERMISSION_CODE);
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// User declined for Background Location Permission.
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, BACKGROUND_LOCATION_PERMISSION_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == LOCATION_PERMISSION_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// User granted location permission
// Now check if android version >= 11, if >= 11 check for Background Location Permission
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED) {
// Background Location Permission is granted so do your work here
} else {
// Ask for Background Location Permission
askPermissionForBackgroundUsage();
}
}
} else {
// User denied location permission
}
} else if (requestCode == BACKGROUND_LOCATION_PERMISSION_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// User granted for Background Location Permission.
} else {
// User declined for Background Location Permission.
}
}
}
How we take permissions is explained in the above code.
After doing above steps, location permission is set to “Allow all the time”.
Points to note:
- If you ask for ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION at once then permission dialog will not shown. First ask for fine location and if user accepts then ask for background location.
- In Android Version lower than 11, ACCESS_BACKGROUND_LOCATION shows a dialog like this:
and in Android 11 and above, it looks like this:
For Android 11 and above, ACCESS_BACKGROUND_LOCATION opens up in settings.
Thanks for reading 🙌
Source Code : https://github.com/MohitSingh2002/location_permission_android_11