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.
12 replies on “Setting Android Settings”
Are these settings global, or do the changes you make only apply to your app? Being able to turn off the SCREEN_OFF_TIMEOUT would be very useful for specific apps, but turning it off globally would be a very bad thing.
These are system wide settings. If you want to keep the screen on for your app you could cache the old timeout and then replace it when your app becomes inactive, or use a WakeLock (I’ll blog about that in the future).
[…] Head over and read Jim’s full blog post about the system settings API. […]
Hi, i need acces to android WiFi settings or enabled the WiFi on start aaplication created in Delphi xe5. Thanks!
Sorry for this question, but what’s is the type of “Result”?
Result is the type of the return value from the function. Integer on the first function and Boolean on the second function.
[…] make use of Conditional Defines in Object Pascal and Predefined Macros in C++. In my blog post on Android Settings I showed how to make a JNI call with Object Pascal, but you can also look at the DeviceInfo Mobile […]
hi i’m from korea (south)
i don’t speak English, but i can struggle.
ok.. question.
I want to change the virtual keyboard type in android and ios.
example, style, keyboard sound, keyboard background, keyboard touch effect.
please…
and I’ll return the favour some time.
thank you
Sorry, I haven’t seen how to make those changes to the settings. It might not be possible without Root or Jailbreak.
Having a hard time using this in XE7. Does anyone have the necessary changes.?
Might be something moved. What is the error?
Try this
http://delphi.org/2014/05/where-is-stringtojstring-and-strtonsstr-in-xe6/
Or this
http://delphi.org/2014/08/how-to-get-answers-to-your-questions/
You are correct. I was being at bit lazy.
The includes needed in XE7 are
Androidapi.Helpers
Androidapi.JNI.Provider
Thanks
Wade