I'm trying to implement a screen management within a phone app.
My target is to switch off the screen when the user put the phone near the ear and switch on when put off the phone from ear.
B4X:
Sub Process Global
Dim ps As PhoneSensors
End Sub
Sub Activity_Create(FirstTime As Boolean)
If FirstTime Then
ps.Initialize(ps.TYPE_PROXIMITY)
ps.StartListening("ps")
End If
End Sub
Sub ps_SensorChanged (Values() As Float)
Log(Values(0))
If Values(0)=0 Then
Log("Screen switch off")
phoneWS.ReleaseKeepAlive
Else
Log("Screen switch on")
phoneWS.KeepAlive(True)
End If
End Sub
I'm running the code to handle the sensor but I it doesn't work as expected : the screen switch on / off isn't excuted.
Any Ideas?
KeepAlive will do nothing if the user turns off the screen. PartialLock will keep the CPU running (it is not clear whether the sensors will keep working or not).
But the real need is handle the screen dim and bright while the App is in the foreground.
I don't undersand why we have to put the code in the service ?