The Shake event works differently on different devices. Special implementation of Shake Event

I am developing a simple Shake function for my application and I am having a strange problem. When I test it on my Honor5x, it works like a charm, but when I tried to execute it on the Samsung S5, it is too sensitive. Is it possible that the accelerometer works differently on different devices? If so, is it possible that the change in condition depends on the accuracy of the device / accelerometer? Here is my code for detecting a Shake event:

  gravity [0] = alpha * gravity [0] + (1 - alpha) * event.values ​​[0];
         gravity [1] = alpha * gravity [1] + (1 - alpha) * event.values ​​[1];

         float accX = event.values ​​[0] - gravity [0];
         float accY = event.values ​​[1] - gravity [1];
         float maxSpeed ​​= Math.max (accX, accY);
         if (maxSpeed> SHAKE_THRESHOLD)
         {
             if (shakesInRow == 0) {
                 shakesInRow ++;
                 shakeTime = System.currentTimeMillis ();
             } else {
                 if ((System.currentTimeMillis () - shakeTime) <SHAKE_TIME_DETECTOR) {
                     shakesInRow ++;
                 } else {
                     shakesInRow = 0;
                     shakeTime = 0;
                     return
                 }
                 if (shakesInRow> = SHAKE_IN_ROW) {
                     shakesInRow = 0;
                     runMainActivity ();
                 }
             }
         }
+5
source share
1 answer

You can try Square library to detect shaking - seismic

They collect samples over a period of time, and if 3/4 of these samples are accelerated, it means that shaking has occurred.

+1
source

All Articles