Categories
Android

Setting Android Settings

On the Android platform all the system wide settings that are accessible via the Settings app are also accessible to your app. You just need to add the uses permission WRITE_SETTINGS. Here is a simple Delphi XE5 example for changing the screen timeout.

First you need the following in your uses clause:

Androidapi.JNI.Provider, // JSettings_SystemClass
FMX.Helpers.Android; // SharedActivityContext

Here is the code to read and set the Screen Off Timeout:

function GetScreenOffTimeout: Integer;
begin
  Result := TJSettings_System.JavaClass.getInt(
    SharedActivityContext.getContentResolver,
    TJSettings_System.JavaClass.SCREEN_OFF_TIMEOUT,
    15000);  // 15 seconds is default is not found
end;

function SetScreenOffTimeout(ATimeOut: Integer): Boolean;
begin
  Result := TJSettings_System.JavaClass.putInt(
    SharedActivityContext.getContentResolver,
    TJSettings_System.JavaClass.SCREEN_OFF_TIMEOUT,
    ATimeOut);
end;

In the GetScreenOffTimeout we pass a default value to use if none is found. I passed in 15000, which is 15 seconds, or the smallest value for my phone. The largest value on my phone is 600000, which is 10 minutes. It appears you can set it to any value, even one that the settings app doesn’t explicitly list as an option.

There are lots of other settings available for your adjustment.