Meaningless Drivel is fun!
The moose likes Android and the fly likes Problem in detecting a shake movement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Problem in detecting a shake movement " Watch "Problem in detecting a shake movement " New topic

Problem in detecting a shake movement

ganesh gupthag

Joined: Mar 05, 2009
Posts: 5
Hi group ,
I use OpenIntents SensorSimulator to detect the shake movement
In code i wrote

public float accelerometer_shake_threshold = 1.2f; // what does this 1.2 means to this program
static final int ACCELEROMETER_FLOAT_TO_INT = 1024; // is this a conversion factor

public void onSensorChanged(int sensor, float[] values){
if( sensor == SensorManager.SENSOR_ACCELEROMETER){
int t = (int)(ACCELEROMETER_FLOAT_TO_INT * accelerometer_shake_threshold);


int ax = (int)(ACCELEROMETER_FLOAT_TO_INT * values[0]);
int ay = (int)(ACCELEROMETER_FLOAT_TO_INT * values[1]);
int az = (int)(ACCELEROMETER_FLOAT_TO_INT * values[2]);

int len2 = ax * ax + ay * ay + az * az;
android.util.Log.i("LEN is",Integer.toString(len2));


Log.v(TAG,"Force Detected");
TextView tv=(TextView)findViewById(;
tv.setText("ViolentForce Detected");

I could not get the desired result ,the value of len2 is seems to be constant for any movement I give through the sensor simulator (Yaw,Roll and Pitch).If the len2 is constant value then what could be the purpose of the if statement .
I just blindly followed the code snippet given under" Detecting "Shake" With The Accelerometer"(
) .I don't no where i went wrong
In other few examples which i came across has len2 value compared with cube root of gravity(being a newb its Greek to me ) Help me out to know the concept to detect a violent shake .What kind of physics is used to detect this sense .
Monu Tripathi

Joined: Oct 12, 2008
Posts: 1369

Accelerometer gives acceleration(raw data) along X-Y-Z axis in SI units. To detect a shake, you need to define it first and then apply a suitable model(can require good understanding of physics).

The algorithm for detecting a shake could be as simple as:

The way you combine your accelerometer readings and the threshold value will define the "strength" and type of "shake" that your code can sense(Perhaps providing this in API would have been more convenient).

Maybe someone with a physics degree can help here.

[List of FAQs] | [Android FAQ] | [Samuh Varta]
ganesh gupthag

Joined: Mar 05, 2009
Posts: 5
Thanks Tripathi for your post
What do you mean by applying a model ,Can't i use OpenIntents Sensor Simulator to pass a shake movement (by just sliding the Yaw or,Roll or Pitch vigorously )
Monu Tripathi

Joined: Oct 12, 2008
Posts: 1369

By model, I meant -

{formula(designed using accelerometer readings) or
predicates on the readings}
that will describe shake for your algorithm.

For e.g: In you code, you chose sum of squares and compared it with some large value:
[ "X^2 + Y^2+Z^2" > "threshold^2"].
I agree. Here's the link:
subject: Problem in detecting a shake movement
It's not a secret anymore!