Android Permission Model

From RAD Studio
Jump to: navigation, search

Recent versions of the Android API have changed the mechanism to request certain permissions, which now need to be requested at runtime as well as specified in the manifest file.

You can call PermissionsService.RequestPermissions (from the System.Permissions.pas unit) to request permissions from the users passing a routine that will be called with the user's responses. Optionally, the permission request routine can also take another routine to explain the need for the permission to the users, in case they have previously denied the request.

The Delphi code snippet below is extracted from Object Pascal/Mobile Snippets/Location:

procedure TLocationForm.swLocationSensorActiveSwitch(Sender: TObject);
begin
{$IFDEF ANDROID}
  if swLocationSensorActive.IsChecked then
    PermissionsService.RequestPermissions([JStringToString(TJManifest_permission.JavaClass.ACCESS_FINE_LOCATION)],
      procedure(const APermissions: TArray<string>; const AGrantResults: TArray<TPermissionStatus>)
      begin
        if (Length(AGrantResults) = 1) and (AGrantResults[0] = TPermissionStatus.Granted) then
          { activate or deactivate the location sensor }
          LocationSensor1.Active := True
        else
        begin
          swLocationSensorActive.IsChecked := False;
          TDialogService.ShowMessage('Location permission not granted');
        end;
      end)
  else
{$ENDIF}
    LocationSensor1.Active := False;
end;

The C++ code snippet below is extracted from CPP/Mobile Snippets/Location:

void __fastcall TForm2::swLocationSensorActiveSwitch(TObject *Sender)
{
#ifdef __ANDROID__
    if (swLocationSensorActive->IsChecked)
    {
        DynamicArray<String> permissions;
        permissions.Length = 1;
        permissions[0] = JStringToString(TJManifest_permission::JavaClass->ACCESS_FINE_LOCATION);

        PermissionsService()->RequestPermissions(permissions,
            [this](const DynamicArray<String> APermissions, const DynamicArray<TPermissionStatus> AGrantResults)
            {
                if ((AGrantResults.Length == 1) and(AGrantResults[0] == TPermissionStatus::Granted))
                	// activate or deactivate the location sensor
                	this->LocationSensor1->Active = true;
                else
                {
                    this->swLocationSensorActive->IsChecked = False;
                    ShowMessage("Location permission not granted");
                }
            });
    }
    else
#endif
        // activate or deactivate the location sensor
        LocationSensor1->Active = false;
}

Actual examples of the use of the new Android permissions model, showcasing best practices, are available in the following demos updated for 10.3:

  • CPP/Mobile Snippets/AccessCameraApp
  • CPP/Mobile Snippets/AudioRecPlay
  • CPP/Mobile Snippets/CameraComponent
  • CPP/Mobile Snippets/CameraRoll
  • CPP/Mobile Snippets/Location
  • CPP/Mobile Snippets/PhoneDialer
  • CPP/Mobile Snippets/ShareSheet
  • CPP/Multi-Device Samples/Device Sensors and Services/Address Book/BirthdayReminder
  • CPP/Multi-Device Samples/Device Sensors and Services/Address Book/Contacts
  • CPP/Multi-Device Samples/Device Sensors and Services/Bluetooth/BLEScanner
  • CPP/Multi-Device Samples/Device Sensors and Services/FlashLight
  • CPP/Multi-Device Samples/Device Sensors and Services/Map Type Selector
  • CPP/Multi-Device Samples/Device Sensors and Services/SensorInfo
  • CPP/Multi-Device Samples/Media/MusicPlayer
  • CPP/Multi-Device Samples/Media/PhotoEditorDemo
  • Object Pascal/Mobile Snippets/AccessCameraApp
  • Object Pascal/Mobile Snippets/AudioRecPlay
  • Object Pascal/Mobile Snippets/CameraComponent
  • Object Pascal/Mobile Snippets/CameraRoll
  • Object Pascal/Mobile Snippets/Location
  • Object Pascal/Mobile Snippets/PhoneDialer
  • Object Pascal/Mobile Snippets/ShareSheet
  • Object Pascal/Multi-Device Samples/Device Sensors and Services/Address Book/BirthdayReminder
  • Object Pascal/Multi-Device Samples/Device Sensors and Services/Address Book/Contacts
  • Object Pascal/Multi-Device Samples/Device Sensors and Services/App Tethering/PhotoWall/Mobile
  • Object Pascal/Multi-Device Samples/Device Sensors and Services/Bluetooth/BLEScanner
  • Object Pascal/Multi-Device Samples/Device Sensors and Services/FlashLight
  • Object Pascal/Multi-Device Samples/Device Sensors and Services/Map Type Selector
  • Object Pascal/Multi-Device Samples/Device Sensors and Services/SensorInfo
  • Object Pascal/Multi-Device Samples/Media/MusicPlayer
  • Object Pascal/Multi-Device Samples/Media/PhotoEditorDemo
  • Object Pascal/RTL/Tethering/PhotoWall/Mobile

Please refer to these demos as a blueprint for updating your own Android code to handle permissions according to the new platform requirements.