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.
There are several mistakes here:
1. Service_Create will not run in this case as the service is probably already running.
2. You need to save the data to a file or it will be lost when the process is killed.
3. You can use CallSubDelayed to notify the service about the change.
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