Android Question How to turn accelerometer in live wallpaper on/off?

Pavka

Member
Licensed User
Hi!
I can't make accelerometer start/stop working in my live wallpaper when user enables/disables it in preferences menu. The code I used:
B4X:
Sub Process_Globals
Dim ps As PhoneSensors
end sub

Sub Service_Create
If accel=True Then'accel=True enables accelerometer, false disables
ps.Initialize(ps.TYPE_ACCELEROMETER)
ps.StartListening("Sensor")
Else
ps.StopListening'I added this after, hoping it will help, but it didn't
End If
end sub

Sub servise_destroy
ps.StopListening
End Sub 

Sub Sensor_SensorChanged (Values() As Float)
    accx=Values(0)'global variables
    accy=Values(1)
end sub
accelerometer works all the time and changing accel has no affect.
First I used another code for long time, but result was similar. I also mentioned that accelerometer does not turn off when service is destroyed because of fast battery draining. This is the code I was using before:
B4X:
Sub Process_Globals
Dim Accelerometer As PhoneAccelerometer
end sub

Sub Service_Create
If accel=True Then'accel=True enables accelerometer, false disables
Accelerometer.StartListening("Accelerometer")
End If
end sub

Sub servise_destroy
Accelerometer.StopListening
end sub

Sub Accelerometer_AccelerometerChanged (Xx As Float, Yy As Float, Zz As Float)
   accx=Xx
   accy=Yy
end if
Guys help me please, I tried everything I could but nothing help, I stuck on this for long time.
 

Pavka

Member
Licensed User
'accel' is changed in preferences menu by checkbox and is passed to wallpaper service:
B4X:
    Sub Activity_Pause (UserClosed As Boolean)
If accel.Checked=True Then
    WallpaperService.accel=True
    Else
    WallpaperService.accel=False
End If
End sub
 
Last edited:

Pavka

Member
Licensed User
My live wallpaper is working well except the accelerometer issue I try to solve now, it is even published on Google play: https://play.google.com/store/apps/details?id=colormoby.colorfuldaisies (accelerometer in it works all the time, enabling/disabling it only change whether its data will be used or not). I use StateManager module in preferences menu to save data. To check 'accel' value in runtime I used
B4X:
Engine.Canvas.DrawText("Accel= "&accel,10,190,Typeface.DEFAULT_BOLD,30,Colors.Blue,"LEFT")
, - 'accel' is changed well.
 

Pavka

Member
Licensed User
Ok, I found and fixed the problem, it was all about my incomplete understanding of process life cycle. Thank you Erel for pointing.
 
Last edited:
Top