Search code examples
androiddelphifiremonkeyandroid-permissions

Delphi/Firemonkey How to call Settings.System.canWrite(context) (Android)


I'm using Delphi 10.3 Community Edition and want to use the WRITE_SETTINGS in my application to set the brightness. I could get it managed to implement this procedure to call the settings dialog:

procedure RequestWriteSettings;
var
  Intent: JIntent;
begin
  Intent := TJIntent.JavaClass.init(TJSettings.JavaClass.ACTION_MANAGE_WRITE_SETTINGS);
  TAndroidHelper.Activity.startActivity(Intent);
end;

I can call this procedure in my application, the dialog appears and I can set the necessary permissions. But I don't want to call this procedure permanently, because that's not user friendly. I need to check if the WRITE_SETTINGS permission is already set, but I don't know how to implement this in Delphi/Firemonkey.

What I could find is that one has to call the "Settings.System.canWrite(context)" function, but I only can find samples for java. Calling these kind of java routines in Delphi isn't that easy. I'm searching around already for some weeks and tried "things on my own", but still without success.

Can someone provide the code line how this routine has to be called in Delphi?

Thanks so much in advance! MPage


Solution

  • Example code for checking WRITE_SETTINGS:

    uses
      Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Provider, Androidapi.JNI.Net, Androidapi.Helpers;
    
    procedure TForm1.RequestWriteSettingsButtonClick(Sender: TObject);
    begin
      if not TJSettings_System.JavaClass.canWrite(TAndroidHelper.Context) then
        StartWritePermissionsActivity
      else
        ShowMessage('System says app can write settings');
    end;
    
    procedure TForm1.StartWritePermissionsActivity;
    var
      LIntent: JIntent;
    begin
      LIntent := TJIntent.JavaClass.init(TJSettings.JavaClass.ACTION_MANAGE_WRITE_SETTINGS);
      LIntent.setData(TJnet_Uri.JavaClass.parse(StringToJString('package:').concat(TAndroidHelper.Context.getPackageName)));
      TAndroidHelper.Context.startActivity(LIntent);
    end;