Android Question setting system settings programatically ?

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   SetScreenOffTimeout(60000)
End Sub

Sub SetScreenOffTimeout(timeout As Int)
   Dim context As JavaObject
   context.InitializeStatic("anywheresoftware.b4a.BA")
   context = context.GetField("applicationContext")
   Dim settings As JavaObject
   settings.InitializeStatic("android.provider.Settings.System")
   settings.RunMethod("putInt", Array As Object(context.RunMethod("getContentResolver", Null), _
     "screen_off_timeout", timeout))
End Sub
The value is in milliseconds. Note that the settings screen is not updated automatically. You might need to "kill" it and then start it again to see the change.

Add this line to the manifest editor:
B4X:
AddPermission(android.permission.WRITE_SETTINGS)
 
Upvote 0

aeropic

Active Member
Licensed User
Longtime User
You can use this code:
Hi Erel,

Many thanks for this fast "sunday" answer, it's incredible to see how present you are on this forum :rolleyes:

The code works like a charm and allows to set the ScreenOffTimeOut to the allowed values in the settings screen
However if you select programmatically another value then the timeout is set up to the nearest truncated value present in the MMI

For example : in the settings screen I can access to 15s, 30s, 1min ... (running on a Nexus 5 with kitkat 4.4.2)

If I setup 40s I get a 30s timeout
if I setup 10s I get 15s

That's a pity as I wanted to have a very short timeout to wake up the screen as less as possible.

Do you know if there is a way to force new values in the System Settings ?

Thanks again
Alain
 
Upvote 0
Top