Android Question Read and Set phone screen-off time

ducphu

Active Member
Licensed User
Longtime User
Hi everyone,

Can anyone show me how to read the current screen-off time setting and change it programmatically.
I checked through Phone library but there is only option to keep the screen always on. Thank you.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   SetScreenTimeout(120000)
   Log(GetScreenTimeout)
End Sub


Sub SetScreenTimeout(Timeout As Int) As Int
   Dim jo As JavaObject
   Return jo.InitializeStatic("android.provider.Settings.System") _
     .RunMethod("putInt", Array As Object(GetContext.RunMethod("getContentResolver", Null) _
     , "screen_off_timeout", Timeout))
End Sub

'Return the timeout measured in milliseconds
Sub GetScreenTimeout As Int
   Dim jo As JavaObject
   Return jo.InitializeStatic("android.provider.Settings.System") _
     .RunMethod("getInt", Array As Object(GetContext.RunMethod("getContentResolver", Null) _
     , "screen_off_timeout"))
End Sub

Sub GetContext As JavaObject
  Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

Manifest editor:
B4X:
AddPermission(android.permission.WRITE_SETTINGS)
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Hi Erel, thanks a lot. The GetScreenTimeOut works great, but when I try SetScreenTimeOut, I got the following error:

java.lang.NumberFormatException: Invalid double: "true"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:94)
at java.lang.StringToReal.parseDouble(StringToReal.java:263)
at java.lang.Double.parseDouble(Double.java:295)
at anywheresoftware.b4a.BA.ObjectToNumber(BA.java:612)
at b4a.example.main._setscreentimeout(main.java:3111)
at b4a.example.main._activity_create(main.java:597)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:174)
at b4a.example.main.afterFirstLayout(main.java:98)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:76)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:5225)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
at dalvik.system.NativeStart.main(Native Method)

My code in Activity_Create sub is

B4X:
If GetScreenTimeout < 120000 Then SetScreenTimeout(120000)
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Ok Solved it. It should be
B4X:
Sub SetScreenTimeout(Timeout AsInt)
not
B4X:
Sub SetScreenTimeout(Timeout AsInt) As Int

Now another issue is, I want to change the screen time out to, let say 120000 when my app is running, and then reset it back to user's setting when my app is closed. I manage to do that if the user click on "Quit" button in my app or click on BACK_KEY., but I cant do it if user click the HOME_KEY, since as I read through some tut that HOME_KEY is not captured. So, is there anyone have solution for my problem?
 
Upvote 0

Embouk

Member
Licensed User
Longtime User
I know this is an old thread but i'm trying to change the screen timeout time but can't get it to work. the code above doesn't seem to work for me. it says in the log 120000 but when i check it on my device it doesn't time out and when i check the display settings it says 2 minutes. any help would be great. thanks in advanced
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1 Second are 1000 ms
120 Second = 2 Minutes
2 minutes = 120000 ms
 
Upvote 0

jeronimovilar

Active Member
Licensed User
Longtime User
Whas is this error:
main_setscreentimeout (java line: 298)

java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:84)
at teste.tela.main._setscreentimeout(main.java:298)
at teste.tela.main._activity_create(main.java:211)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at teste.tela.main.afterFirstLayout(main.java:84)
at teste.tela.main.access$100(main.java:16)
at teste.tela.main$WaitForLayout.run(main.java:72)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Permission Denial: writing com.android.providers.settings.SettingsProvider uri content://settings/system from pid=568, uid=10038 requires android.permission.WRITE_SETTINGS
at android.os.Parcel.readException(Parcel.java:1322)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:408)
at android.content.ContentResolver.insert(ContentResolver.java:604)
at android.provider.Settings$NameValueTable.putString(Settings.java:575)
at android.provider.Settings$System.putString(Settings.java:771)
at android.provider.Settings$System.putInt(Settings.java:854)
... 20 more
java.lang.reflect.InvocationTargetException

----------------------------------------------------------
MANIFEST EDITOR:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" />
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
AddPermission(android.permission.WRITE_SETTINGS)
'End of default text.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Caused by: java.lang.SecurityException: Permission Denial: writing com.android.providers.settings.SettingsProvider uri content://settings/system from pid=568, uid=10038 requires android.permission.WRITE_SETTINGS

But as you wrote it is already in your manifest.
So i don´t know what next, sorry.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It does work here, on a Nexus 5 device running Android 6:
B4X:
Sub SetScreenTimeout(Timeout As Int)
  Dim jo As JavaObject
  Dim context As JavaObject
  context.InitializeContext
  jo.InitializeStatic("android.provider.Settings.System") _
  .RunMethod("putInt", Array As Object(context.RunMethod("getContentResolver", Null) _
  , "screen_off_timeout", Timeout))
End Sub
 
Upvote 0

jeronimovilar

Active Member
Licensed User
Longtime User
with new sub(setscreentimeout):

Compiling code. Error
Error compiling program.
Error description: Unknown member: initializecontext
Occurred on line: 23
context.InitializeContext
Word: initializecontext
 
Upvote 0

jeronimovilar

Active Member
Licensed User
Longtime User
thanks
It´s working fine: (sorry my english)

Sub SetScreenTimeout(Timeout As Int)
Dim jo As JavaObject
Return jo.InitializeStatic("android.provider.Settings.System") _
.RunMethod("putInt", Array As Object(GetContext.RunMethod("getContentResolver", Null) _
, "screen_off_timeout", Timeout))
End Sub
 
Upvote 0
Top