public void onSensorChanged(SensorEvent event) {
          // use the event timestamp as reference
          // so the manager precision won't depends
          // on the AccelerometerListener implementation
          // processing time
          now = event.timestamp;

          x = event.values[0];
          y = event.values[1];
          z = event.values[2];

          // if not interesting in shake events
          // just remove the whole if then else block
          if (lastUpdate == 0) {
            lastUpdate = now;
            lastShake = now;
            lastX = x;
            lastY = y;
            lastZ = z;
            Toast.makeText(aContext, "No Motion detected", Toast.LENGTH_SHORT).show();

          } else {
            timeDiff = now - lastUpdate;

            if (timeDiff > 0) {

              /*force = Math.abs(x + y + z - lastX - lastY - lastZ)
              / timeDiff;*/
              force = Math.abs(x + y + z - lastX - lastY - lastZ);

              if (Float.compare(force, threshold) > 0) {
                // Toast.makeText(Accelerometer.getContext(),
                // (now-lastShake)+"  >= "+interval, 1000).show();

                if (now - lastShake >= interval) {

                  // trigger shake event
                  listener.onShake(force);
                } else {
                  Toast.makeText(aContext, "No Motion detected", Toast.LENGTH_SHORT).show();
                }
                lastShake = now;
              }
              lastX = x;
              lastY = y;
              lastZ = z;
              lastUpdate = now;
            } else {
              Toast.makeText(aContext, "No Motion detected", Toast.LENGTH_SHORT).show();
            }
          }
          // trigger change event
          listener.onAccelerationChanged(x, y, z);
        }
Пример #2
0
        public void onSensorChanged(SensorEvent event) {

          now = event.timestamp;

          if (now - lastUpdate > pauze_ns) {
            x = event.values[0];
            y = event.values[1];
            z = event.values[2];

            // trigger change event
            listener.onAccelerationChanged(x, y, z);
            lastUpdate = now;
          }
        }