Catching Screen Off / On in an Activity?

boten

Active Member
Licensed User
Longtime User
My app has a timer (controls a changing graphics on the screen).
I want the timer to "pause" when the screen goes to sleep, so I used:

B4X:
Sub Process_Globals
Dim tmr As Timer : tmr.Initialize("adv",100) : tmr.Enabled=False
Dim PhnEv As PhoneEvents
End Sub

Sub Activity_Resume
PhnEv.Initialize("event")
tmr.Enabled=True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
PhnEv.StopListening
tmr.Enabled=False
End Sub

Sub event_ScreenOff(Int1 As Intent)
tmr.Enabled=False
End Sub

Sub event_ScreenOn(Int1 As Intent)
tmr.Enabled=True
End Sub

I tested it, but after the screen "goes to sleep" and I press the HOME key to "wake it up" I got the error:

java.lang.IllegalArgumentException: Receiver not registered: anywheresoftware.b4a.phone.PhoneEvents$16@487b2f50

That happened in Activity_Resume.

So, how do I pasue/start the timer on Screen Off/On in an activity (not a service)
 

boten

Active Member
Licensed User
Longtime User
my bad. forgot that when the screen goes off the app is paused.:sign0161:
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…