With newer releases of Android, you must explicitly request location permissions on Andriod. Users can optionally accept or refuse these permissions for the app.
On Cordova and Xamarin, the SDK prompts users for permissions by default. However, on native Android, users need to request the permissions.
To request permissions for location on Android, do the following:

  1. Enable locations in an Activity so that the parent element can display the child permissions dialog.
  2. Add a variable to your Activity as follows:
    private static final int REQUEST_LOCATION = 123;
    The value of the variable must be unique as it is used to distinguish this request from other permission requests.
  3. Replace the call to LocationManager.enableLocationSupport with the following code:
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
    ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) != PackageManager.PERMISSION_GRANTED ||
    ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_ADMIN) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this,
    new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.ACCESS_COARSE_LOCATION,
    Manifest.permission.BLUETOOTH,
    Manifest.permission.BLUETOOTH_ADMIN}, REQUEST_LOCATION);
    } else {
    System.out.println("Location permissions available, starting location");
    LocationManager.enableLocationSupport(getApplicationContext())
    }
  4. Ensure android.support.v4.app.ActivityCompat and android.support.v4.content.ContextCompat are imported along with android.Manifest and android.content.pm.PackageManager.
  5. Add the following method to your Activity:
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if(requestCode == REQUEST_LOCATION) {
    if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    System.out.println("Location permissions granted, starting location");
    LocationManager.enableLocationSupport(getApplicationContext());
    }
    }
    }

Introduce a new dialog
You can also introduce a new dialog to prompt users before opening the Android permissions dialog. In this dialog, you can explain why the user might want to grant permissions, and request their consent. If they agree, the Android dialog can be displayed to enable location support. Even if a user does not currently want to share location, you can request it again later when required.

Join The Discussion

Your email address will not be published. Required fields are marked *