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

Discussion in 'Android Questions' started by Pavka, Jun 4, 2015.

  1. Pavka

    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:
    Code:
    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:
    Code:
    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.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    I don't see how your code can work. Where is 'accel' changed?
     
  3. Pavka

    Pavka Member Licensed User

    'accel' is changed in preferences menu by checkbox and is passed to wallpaper service:
    Code:
    Sub Activity_Pause (UserClosed As Boolean)
    If accel.Checked=True Then
        WallpaperService.accel=
    True
        
    Else
        WallpaperService.accel=
    False
    End If
    End sub
     
    Last edited: Jun 5, 2015
  4. Erel

    Erel Administrator Staff Member Licensed User

    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.
     
  5. Pavka

    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
    Code:
    Engine.Canvas.DrawText("Accel= "&accel,10,190,Typeface.DEFAULT_BOLD,30,Colors.Blue,"LEFT")
    , - 'accel' is changed well.
     
  6. Pavka

    Pavka Member Licensed User

    Erel, can you help me please?
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    See my previous answer.
     
  8. Pavka

    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: Jul 1, 2015
Loading...