Android Question Phone screen timeout problem

saeed10051

Active Member
Licensed User
Longtime User
Hi,
I am making an application, using a timer, the application timer works for 60 seconds but before the 60 seconds pass my phone screen time out and turn off as the screen time out is 45 seconds.
How can i keep the screen on until the timer is working
 

saeed10051

Active Member
Licensed User
Longtime User
i have tried the option provided in following thread to set the screen out time
Getscreentimeout method is working fine
but the setscreentimeout method is giving me following error
Error occurred on line: 117 (Main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:777)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:354)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.main.afterFirstLayout(main.java:105)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7814)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: java.lang.SecurityException: b4a.example was not granted this permission: android.permission.WRITE_SETTINGS.
at android.os.Parcel.createException(Parcel.java:2088)

it is saying that runtime permission write-settings is not granted, however i have given this permission in my manifest file.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
[Erel] The CheckAndRequest method can only be called from an Activity.

https://www.b4x.com/android/forum/t...ons-android-6-0-permissions.67689/post-428719
when i call it from my activity method create first time, it is returning following error

Error occurred on line: 119 (Main)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
at b4a.example.main._setscreentimeout(main.java:481)
at b4a.example.main._activity_create(main.java:440)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:351)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at b4a.example.main.afterFirstLayout(main.java:105)
at b4a.example.main.access$000(main.java:17)
at b4a.example.main$WaitForLayout.run(main.java:83)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7814)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
If CanWriteToSystemSettings = False Then
Dim in As Intent
in.Initialize("android.settings.action.MANAGE_WRITE_SETTINGS", "package:" & Application.PackageName)
StartActivity(in)
Wait For Activity_Resume
If CanWriteToSystemSettings = False Then
Log("no permission...")
Return
End If

should i write above code in sub activity start first time
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Have you tried using the PhoneWakeState:

B4X:
Dim PWS As PhoneWakeState      


PWS.KeepAlive(True)        'keep screen on while we do things (don't let screen timeout)

....do your timer work here....

PWS.ReleaseKeepAlive    'let the phone go to sleep now

Keep in mind, if you forget to do the .ReleaseKeepAlive, then the user's battery will die quickly.
 
Upvote 0

saeed10051

Active Member
Licensed User
Longtime User
Have you tried using the PhoneWakeState:

B4X:
Dim PWS As PhoneWakeState     


PWS.KeepAlive(True)        'keep screen on while we do things (don't let screen timeout)

....do your timer work here....

PWS.ReleaseKeepAlive    'let the phone go to sleep now

Keep in mind, if you forget to do the .ReleaseKeepAlive, then the user's battery will die quickly.
Great
It is working now.
Only one small point that the phone immediately sleep after the timer is done. can i keep it running for some 10 more seconds
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Great
It is working now.
Only one small point that the phone immediately sleep after the timer is done. can i keep it running for some 10 more seconds
Maybe you can simulate a keypress to reset the devices screen timeout counter:
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
You can also try doing a .KeepAlive(False) before calling .ReleaseKeepAlive that might prevent the device from immediately going to sleep when you call .ReleaseKeepAlive
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Another way could be to set the timer at 10s intervals. Using a counter, when it reaches 60s you do what it's needed; then on next tick (10s later) you release the lock and turn off (or whatever).
 
Upvote 0
Top