Detect direction of shake movement in SensorListener

by TsEMaNN » Fri, 14 Jan 2011 21:36:23 GMT

Sponsored Links
 Hello developers,

I have got a problem concerning the SensorListener. At the moment I
can already detect if the phone got shaken. Addtionally I now want to
detect in which direction the phone is shaken. Do you maybe know which
values I have to consider to detect this?

My code at the moment looks like this:

public class ShakeActivity extends Activity implements SensorListener
   // For shake motion detection.
   private SensorManager sensorMgr;
   private long lastUpdate = -1;
   private float x, y, z;
   private float last_x, last_y, last_z;
   private float raw_x, last_raw_x;
   private static final int SHAKE_THRESHOLD = 800;
   private final float shakeThreshold = 1.5f;

   protected void onCreate(Bundle savedInstanceState) {

      // start motion detection
      sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
      boolean accelSupported = sensorMgr.registerListener(this,

      if (!accelSupported) {
         // on accelerometer on this device

   protected void onPause() {
      if (sensorMgr != null) {
         sensorMgr = null;

   public void onAccuracyChanged(int arg0, int arg1) {
      // TODO Auto-generated method stub

   public void onSensorChanged(int sensor, float[] values) {
      Log.d("sensor", "onSensorChanged: " + sensor);
      if (sensor == SensorManager.SENSOR_ACCELEROMETER) {
         long curTime = System.currentTimeMillis();
         // only allow one update every 100ms.
         if ((curTime - lastUpdate) > 100) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            x = values[SensorManager.DATA_X];
            y = values[SensorManager.DATA_Y];
            z = values[SensorManager.DATA_Z];
            raw_x = SensorManager.GRAVITY_EARTH;

            float xChange = Math.abs(x - last_x);
            float speed = Math.abs(x + y + z - last_x - last_y -
                  / diffTime * 10000;

            float deltaX = Math.abs(last_x - x);
            float deltaY = Math.abs(last_y - y);
            float deltaZ = Math.abs(last_z - y);
            float delta_raw_x = raw_x - last_raw_x;
            // Log.d("sensor", "diff: " + diffTime + " - speed: " +
            if (speed > SHAKE_THRESHOLD) {
               Log.d("sensor", "shake detected w/ speed: " + speed);
                     "shake detected w/ speed: " + speed + "; accelX:
                           + xChange, Toast.LENGTH_SHORT).show();
               // now left,right,up,down movement has to be detected
               // somehow
            last_x = x;
            last_y = y;
            last_z = z;
            last_raw_x = raw_x;

Best regards,


Re: Detect direction of shake movement in SensorListener

by Hari Edo » Sat, 15 Jan 2011 03:33:47 GMT

 What is your code giving you that's unsatisfactory?

Depending on your definition of "shake", you could probably
tell the difference between "side to side" vs "up and down"
but you will likely get very inconsistent results if you try to
distinguish "leftward" vs "rightward".  Your hand often
anticipates a little, moving left before a big rightward jerk.
(Google "anticipation animation" for more.)


Sponsored Links

Re: Detect direction of shake movement in SensorListener

by bklik » Mon, 21 Feb 2011 03:53:39 GMT

 I think what you're after is the difference in values between X (left
and right), Y (up and down), Z (toward you and away).

My code looks something like this:

public void onSensorChanged(SensorEvent event) {
    long curTime = System.currentTimeMillis();

    x = Math.abs(event.values[0]);
    y = Math.abs(event.values[1]);
    z = Math.abs(event.values[2]);

    if((x+y+z) > ACCEL_THRESHOLD){
        shakeLevel += (x+y+z);
        if(shakeCount >= 3){
            shakeCount = 0;
            shakeLevel = 0;

        if(shakeLevel > SHAKE_THRESHOLD){
            // Do something because the phone has been shaken.

What I'm doing is looking for repetitive amounts of movement in any
direction, over a certain threshold, over time. You could break this
down into counts for X, Y, and Z rather then adding them all together
like I am. Then, just play with different thresholds that trigger when
you expect.



Other Threads

1. Can't dispatch DDM chunk 52454151: no handler defined


Im new to android and after installing SDK and all plugins to the
eclipse ad trying to run sample app (hello worl) I have an emulator
opened, with all keyboard etc. with android_ thext  and after few
minutes there appears something like screensaver.

No app is loaded.

In the message tab in eclipse i Have message:

Starting activity com.Android.HelloAndroid on device
[2010-04-02 16:44:36 - HelloAndroid] ActivityManager: DDM dispatch reg
wait timeout
[2010-04-02 16:44:36 - HelloAndroid] ActivityManager: Can't dispatch
DDM chunk 52454151: no handler defined
[2010-04-02 16:44:36 - HelloAndroid] ActivityManager: Can't dispatch
DDM chunk 48454c4f: no handler defined
[2010-04-02 16:44:36 - HelloAndroid] ActivityManager: Can't dispatch
DDM chunk 46454154: no handler defined
[2010-04-02 16:44:36 - HelloAndroid] ActivityManager: Can't dispatch
DDM chunk 4d505251: no handler defined
[2010-04-02 16:44:46 - HelloAndroid] Device not ready. Waiting 3
seconds before next attempt.

I was looking for an answer but didnt find anything usefull.

Thanks for help


2. huawei u8230 (rbm2) sdk not working


ive recently got a u8230 for developing purposes, but i cant seem to
get it to work with adb. im developing on ubuntu, and as i know that
youve got to edit the /etc/udev/rules.d/51-android.permission file, i
did exactly the same as i did when i developed on my htc magic. well,
after 2 hours it still didnt work, so i changed to ma mac, where it
wasnt working either.

so, is there someone here who develops on that device or can help me
out with another vendor id than provided in the sdk documentation (the
one for htc didnt work for me either, but i found a site with a
different one that worked for the magic)

thanks in advance

oh, and yes, usb debugging IS turned on, yes it is connected, yes i
know its only 1.5 and i shouldnt develop on that old junk, but i have
to. and no, im not going to use my magic for that...


3. Location listening, GPS and sleeping bots

4. javax.xml.bind.annotation.XmlAccessType and Android

5. Create replacement lock screen.

6. Remove other Instances of Application

7. How to control camera capture functions using c/c++ code on JNI level?