Skip to content

Commit c2a1706

Browse files
Android 11+ Background Location Permission Flow Update (#4336)
Fixed #3504 * Update Android background location permission flow for API 30+ - Implement special handling for `ACCESS_BACKGROUND_LOCATION` on API >= 30 in `AndroidImplementation.java`. - Bypass `ActivityCompat.requestPermissions` and guide users to Settings. - Add localization keys for new dialog strings. - Update developer guide with instructions on new behavior and build hint requirements. * Update Android background location permission flow for API 30+ - Implement special handling for `ACCESS_BACKGROUND_LOCATION` on API >= 30 in `AndroidImplementation.java`. - Bypass `ActivityCompat.requestPermissions` and guide users to Settings. - Add localization keys for new dialog strings. - Update developer guide with instructions on new behavior and build hint requirements. * Update Android background location permissions section Clarified the process for requesting background location permissions on Android 11 and higher, including details about Codename One's handling of the flow and customization options. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Shai Almog <[email protected]>
1 parent f7f6ace commit c2a1706

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Ports/Android/src/com/codename1/impl/android/AndroidImplementation.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10898,6 +10898,36 @@ public static boolean checkForPermission(String permission, String description,
1089810898
if(android.os.Build.VERSION.SDK_INT < 23){
1089910899
return true;
1090010900
}
10901+
10902+
if (android.os.Build.VERSION.SDK_INT >= 30 && "android.permission.ACCESS_BACKGROUND_LOCATION".equals(permission)) {
10903+
if (android.support.v4.content.ContextCompat.checkSelfPermission(getContext(), permission) == PackageManager.PERMISSION_GRANTED) {
10904+
return true;
10905+
}
10906+
if (getActivity() == null) {
10907+
return false;
10908+
}
10909+
10910+
String prompt = Display.getInstance().getProperty(permission, description);
10911+
String title = Display.getInstance().getProperty("android.permission.ACCESS_BACKGROUND_LOCATION.title", "Requires permission");
10912+
String settingsBtn = Display.getInstance().getProperty("android.permission.ACCESS_BACKGROUND_LOCATION.settings", "Settings");
10913+
String cancelBtn = Display.getInstance().getProperty("android.permission.ACCESS_BACKGROUND_LOCATION.cancel", "Cancel");
10914+
10915+
if(Dialog.show(title, prompt, settingsBtn, cancelBtn)){
10916+
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
10917+
Uri uri = Uri.fromParts("package", getContext().getPackageName(), null);
10918+
intent.setData(uri);
10919+
getActivity().startActivity(intent);
10920+
10921+
String explanationTitle = Display.getInstance().getProperty("android.permission.ACCESS_BACKGROUND_LOCATION.explanation_title", "Permission Required");
10922+
String explanationBody = Display.getInstance().getProperty("android.permission.ACCESS_BACKGROUND_LOCATION.explanation_body", "Please enable 'Allow all the time' in the settings, then press OK.");
10923+
String okBtn = Display.getInstance().getProperty("android.permission.ACCESS_BACKGROUND_LOCATION.ok", "OK");
10924+
10925+
Dialog.show(explanationTitle, explanationBody, okBtn, null);
10926+
return android.support.v4.content.ContextCompat.checkSelfPermission(getActivity(), permission) == PackageManager.PERMISSION_GRANTED;
10927+
} else {
10928+
return false;
10929+
}
10930+
}
1090110931

1090210932
String prompt = Display.getInstance().getProperty(permission, description);
1090310933

docs/developer-guide/Miscellaneous-Features.asciidoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,12 @@ LocationManager.getLocationManager()
450450
.addGeoFencing(GeofenceListenerImpl.class, gf);
451451
----
452452

453+
===== Android Background Location Permissions (API 30+)
454+
455+
On Android 11 (API level 30) and higher, requesting background location permission requires a two-step process. First, foreground location permissions must be granted. Then, the app must request background location access, which will direct the user to the system settings to select "Allow all the time". Codename One handles this flow automatically when you use `LocationManager`.
456+
457+
For Android 11+ (API 30+), Codename One detects if background location is needed and presents a dialog explaining the requirement before redirecting the user to the app settings. You can customize the permission prompt message using the localization key `android.permission.ACCESS_BACKGROUND_LOCATION`.
458+
453459
[source,java]
454460
----
455461
public class GeofenceListenerImpl implements GeofenceListener {

0 commit comments

Comments
 (0)