Android-Berechtigungsmodell

Aus RAD Studio
Wechseln zu: Navigation, Suche

In neueren Versionen der Android-API wurde der Mechanismus zur Anforderung bestimmter Berechtigungen geändert. Diese müssen jetzt zur Laufzeit angefordert sowie in der Manifestdatei angegeben werden.

Sie können PermissionsService.RequestPermissions (aus der Unit System.Permissions.pas) aufrufen, um Berechtigungen von den Benutzern anzufordern, indem Sie eine Routine übergeben, die mit der Antwort des Benutzers aufgerufen wird. Falls die Benutzer die Anforderung ablehnen, kann die Routine zur Berechtigungsanforderung ihnen optional anhand einer anderen Routine erklären, warum Berechtigungen erforderlich sind.

Das folgende Delphi-Codefragment wurde aus Object Pascal/Mobile Snippets/Location extrahiert:

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;

Das folgende C++-Codefragment wurde aus CPP/Mobile Snippets/Location extrahiert:

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;
}

Konkrete Beispiele der Verwendung des neuen Android-Berechtigungsmodells, die die Best Practices zeigen, finden Sie in den folgenden, für 10.3 aktualisierten Demos:

  • 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

Nutzen Sie diese Demos als Vorlage für die Aktualisierung Ihres Android-Codes, um die Berechtigungen gemäß den neuen Plattformanforderungen zu handhaben.