1

I've been using this code Android: I want to shake it to implement a shake movement detector. But actually when i run it on my smartphone, it doesn't do anything. I tried to use breakpoints to check , and it never enters in if (mAccel > 12). I tried to shake it really hard, but still don't recognize. I thought that it needs some permission, and i added this line to Manifest:

<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />

I tried in galaxy s3 and OnePlus One, i forgot something?

EspecificExerciseActivity.java

public class EspecificoExercicio extends Activity {

    private ArrayList<Exercicio> listaExercicios;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_especifico_exercicio);

        /* do this in onCreate */
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        mAccel = 0.00f;
        mAccelCurrent = SensorManager.GRAVITY_EARTH;
        mAccelLast = SensorManager.GRAVITY_EARTH;

        if (mAccel > 12) {
            Toast toast1 = Toast.makeText(getApplicationContext(), "Device has shaken.", Toast.LENGTH_LONG);
            toast1.show();
        }
    }

      /* put this into your activity class */
      private SensorManager mSensorManager;
      private float mAccel; // acceleration apart from gravity
      private float mAccelCurrent; // current acceleration including gravity
      private float mAccelLast; // last acceleration including gravity

      private final SensorEventListener mSensorListener = new SensorEventListener() {

        public void onSensorChanged(SensorEvent se) {
          float x = se.values[0];
          float y = se.values[1];
          float z = se.values[2];
          mAccelLast = mAccelCurrent;
          mAccelCurrent = (float) Math.sqrt((double) (x*x + y*y + z*z));
          float delta = mAccelCurrent - mAccelLast;
          mAccel = mAccel * 0.9f + delta; // perform low-cut filter
        }

        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            if (mAccel > 4) {
                Toast toast1 = Toast.makeText(getApplicationContext(), "DevSSDAASSADA", Toast.LENGTH_LONG);
                toast1.show();
            }
        }


      };

      @Override
      protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);

      }

      @Override
      protected void onPause() {
        mSensorManager.unregisterListener(mSensorListener);
        super.onPause();
      }

}
Community
  • 1
  • 1
Marco
  • 625
  • 3
  • 10
  • 30

1 Answers1

1

You're checking if (mAccel > 12) inside your onCreate(). Move the check to onSensorChanged().

Steelight
  • 3,347
  • 1
  • 20
  • 17
  • Still the same @Sleelight – Marco Jan 04 '15 at 14:02
  • If you put a breakpoint in onSensorChanged(), do you get there? – Steelight Jan 04 '15 at 14:05
  • yeas @Steelight, but it doesn't enter on the if. Shoud a i change the value, instead 12, put less? – Marco Jan 04 '15 at 14:12
  • actually i changed the value to 2 instead 12, and the device is recognizing it perfectly! :) – Marco Jan 04 '15 at 14:14
  • I would try printing the values you get to and see. maybe reduce your low cut filter value if you expect a longer shake, or reduce the expected value if you expect a shorter shake. Keep in mind that different phones may have more or less sensitive accelerometers. – Steelight Jan 04 '15 at 14:15