ShakeDetector: Shake it baby!

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
version 1.0
Introduction

This class does exactly what is says and is the simplest class I've ever made.

Latest package (library files and samples)
https://www.dropbox.com/s/p5exn94rle6f2w1/shakedetector.zip

Compiled samples (APK)
https://www.dropbox.com/s/ou4jexl4xj6nz2k/sample.apk


How to use

Declare a ShakeDetector in Sub Globals
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Detector As ShakeDetector
End Sub
Initialize the object in Sub Activity_Create
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")

    Detector.Initialize("Detector")
End Sub
Start the Detector when the activity is resumed
B4X:
Sub Activity_Resume
    Detector.Start
End Sub
Stop the Detector when the activity is paused
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    Detector.Stop
End Sub
Catch the shake event
B4X:
Sub Detector_Shake
    Log("Shake it baby!")
End Sub


Version history:
  • 1.0: Initial version

That's all for now folks! :D
 
Last edited:

ivan.tellez

Active Member
Licensed User
Longtime User
This is really handy, but not really suitable for all kind of projects.

To have a true reusable component, it has to have some kind of adjustment. Maybe if you add a "threshold" property.

Also could be more useful if you could have some feedback of the shake, like returning the velocity.

Thanks for sharing :D
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
You are welcome :)

The calculation inside the library catches a normal shake event based on x,y,z. Normal projects are ok with that. If someone needs to make an app that will do things based on the velocity and so on, it will not work. But these projects are outside of the scope of the library :)
 

Periklis Koutsogiannis

Active Member
Licensed User
Longtime User
For example, I have never seen an app that has a "Do something on shake" to offer any shake calibration options :p
 

ivan.tellez

Active Member
Licensed User
Longtime User
For example, I have never seen an app that has a "Do something on shake" to offer any shake calibration options :p

Actually there are lots of them, some had a range, others just a lo, medium and hi options.

Othes shoul include the options, Like WeChat, you had to hit the phone to be recognized as a shake :confused:, some time ago they change the sensibility and now its fine.


But, I was not really aiming to make an option for the user, it was more an option for the developer to adjust the threshold acording to the each app. :D
 

Douglas Farias

Expert
Licensed User
Longtime User
u dont stop? xD go make 200 + libs xD
 

MiguelAlvarez

Member
Licensed User
Longtime User
That's all for now folks! :D

It does not work from a service?
Can you help me. I need to work from a service running in background.... Thanks
 

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Hello,
does it work if the screen is turned off?
 

stanks

Active Member
Licensed User
Longtime User
i tried it but it won't work. b4x v5.8. Erel's example on the other hand works only when sound is on
 
Top