Android Tutorial Android shake event with PhoneSensors

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:
B4X:
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(0, 255), Rnd(0, 255), Rnd(0, 255))
    sounds.Play(bounceId, 1, 1, 1, 0, 1)
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:

shake_1.png


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.
 

Attachments

  • Shake.zip
    27.9 KB · Views: 3,467

Beja

Expert
Licensed User
Longtime User
If you read carefully Erel's post you could see that he chooses to use the X value only. If you want to handle all values you'll have to pass them too

Thanks lemonisdead,
That was it.
 

Beja

Expert
Licensed User
Longtime User
@eps
Thanks eps.. Erel responded to the X variable only, and that's why all other directions didn't work.
I overlooked this and lemonisdead pointed me to it.
 

ilan

Expert
Licensed User
Longtime User
is it possible to listen to the shake event from a service?
would like to turn on my screen when the phone is locked
 

MiguelAlvarez

Member
Licensed User
Longtime User
The same code will work from a service.
Using from a service in Main
Sub sensor_SensorChanged (Values() As Float)
Shake.HandleSensorEvent(Values)
End Sub
got the :
Error description: Unknown member: handlesensorevent
Occurred on line: 109
Shake.HandleSensorEvent(Values)
Word: handlesensorevent
 

cambopad

Active Member
Licensed User
Longtime User
If I use this in a service that run for a very long time, will it affect the phone performance and battery????
 

stanks

Active Member
Licensed User
Longtime User
when sound is off nothing happens...S3 I9300
i noticed one other big problem. shaking kills my device UI and slow it a lot. restarting ui does not have any effect. i have to flash again ROM!. running apps is ok.
anyone have problems with that?

thanks
 

rtek1000

Active Member
Licensed User
Longtime User
The problem is of course to decide if the stream of values represents a shake.

TYPE_LINEAR_ACCELERATION
added in API level 9
int TYPE_LINEAR_ACCELERATION
A constant describing a linear acceleration sensor type.

See SensorEvent.values for more details.

Constant Value: 10 (0x0000000a)

It may be simpler to check a shake with this data type (TYPE_LINEAR_ACCELERATION).

Source: https://developer.android.com/reference/android/hardware/Sensor?hl=fr#TYPE_LINEAR_ACCELERATION

B4X:
sensor.Initialize(10) ' 10: TYPE_LINEAR_ACCELERATION
 
Top