Android Tutorial Android shake event with PhoneSensors

Discussion in 'Tutorials & Examples' started by Erel, Jun 23, 2011.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Using the phone accelerometer it is possible to handle shake events.
    The problem is of course to decide if the stream of values represents a shake.

    The Shake code module finds shake patterns and raises a ShakeEvent when such occurs.

    Using the Shake module is simple. Add Shake.bas code module to your project (Project - Add Existing module) and then you should add code similar to:
    Code:
    Sub Process_Globals
        
    Dim sensor As PhoneSensors
        
    Dim sounds As SoundPool
        
    Dim bounceId As Int
    End Sub

    Sub Globals

    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    If FirstTime Then
            sensor.Initialize(sensor.TYPE_ACCELEROMETER)
            Shake.CallBackActivity = 
    "Main" 'Set the activity that handles the Shake event
            sounds.Initialize(1)
            bounceId = sounds.Load(
    File.DirAssets, "break.mp3")
        
    End If
    End Sub

    Sub Activity_Resume
        sensor.StartListening(
    "sensor")
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
        sensor.StopListening
    End Sub

    'Delegate the event handling to the Shake module
    Sub sensor_SensorChanged (Values() As Float)
        Shake.HandleSensorEvent(Values)
    End Sub

    'This event is raised when a shake is detected
    Sub ShakeEvent
        
    Activity.Color = Colors.RGB(Rnd(0255), Rnd(0255), Rnd(0255))
        sounds.Play(bounceId, 
    11101)
    End Sub
    You should add a reference to the Phone library. This code also uses a SoundPool object to play a sound. So the Audio library is also required here.

    Shake module also supports recording the sensors data to a CSV file.

    This is useful for understanding the required pattern.
    By recording the data and creating the following Excel graph:

    [​IMG]

    I decided to use the change in the X acceleration (derivative line).

    The attached program plays a sounds and changes the screen color when you shake the phone. It is not always easy to make a shake event. Playing a sound is a good cue for the user that the shake was "successful".

    To prevent repetitive shake events, shakes are disabled for 1.5 seconds after a shake event.
     

    Attached Files:

    MarkusR, Johan Hormaza, dost and 6 others like this.
  2. agraham

    agraham Expert Licensed User

    :)

    Yet again the sound isn't playing on my SanFrancisco/ZTE Blade :( Looks like a problem, I'll try to take a look.
     
    luke2012 likes this.
  3. agraham

    agraham Expert Licensed User

    Not sure if this is the right place but I've looked the SoundPool problem. The mp3 files load into the SoundPool OK as I get a valid ID, but on trying to play I get a zero (error) return and a Logcat entry "Sample 1 not ready". Even waiting several seconds before playing it never seems to become ready. What is odd is that the mp3 sounds play fine in a media player

    From Googling it seems that SoundPool is rather buggy and unpredicable and is more stable when using ogg files. While this seemed a bit like an urban myth I located a short ogg file and tried that instead of the mp3 and it worked!

    Does anyone know of a source of short sound effect files in ogg format?
     
  4. LineCutter

    LineCutter Active Member Licensed User

    Use the MP3s & process them through "Format Factory" or similar to create OGGs from them?
     
  5. mcmanu

    mcmanu Active Member Licensed User

    Very nice :) is it possible to set the sensibility of the sensor?
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. Big JR

    Big JR Member Licensed User

    I know that you can't simulate a shake event on the emulator but when I load the shake example above into API 8 it hangs and eventually reports Force Close. Is that an issue with the emulator or with Froyo? I haven't got a Froyo device to try it on.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    This is most probably an issue with the emulator. This code was first written with a Froyo device.
     
  9. Big JR

    Big JR Member Licensed User

    Thanks Erel. I'm using the shake code in an app and didn't want to unnecessarily restrict it to Gingerbread and beyond.
     
  10. jesustarre

    jesustarre Member Licensed User

    Hello guys, I just tried this module and work excellent!

    How can I let to the user set the shake sensibility?

    Thanks in advance
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    There are several parameters defined in the code module. You can change them from your UI.
     
  12. jesustarre

    jesustarre Member Licensed User

    I got it!

    Just playing with Shake.MagnitudeThreshold

    Thanks!
     
  13. chrjak

    chrjak Active Member Licensed User

    Lol. XD
    I don't understand the values...
    Regards
     
  14. chrjak

    chrjak Active Member Licensed User

    Isn't there a permission for the sensors?
     
  15. Erel

    Erel Administrator Staff Member Licensed User

  16. chrjak

    chrjak Active Member Licensed User

    ok, thx Erel
     
  17. shaxboz

    shaxboz Member Licensed User

    Ey guys, How can use shake events with service module?
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    The same code will work from a service.
     
  19. Beja

    Beja Expert Licensed User

    Erel,
    The example is working when the phone is shaken sideways only. (left-right)
    forward-backward, up-down, spinning are all not working.
     
  20. eps

    eps Well-Known Member Licensed User

    What X, Y and Z values are you seeing? What device are you using?
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice