Implementación

Para instanciar ambos sensores en el onCreate:

// Get the sensors to usemSensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);mSensorAcc=mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);mSensorGyr=mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

// References:// - http://jasonmcreynolds.com/?p=388// - http://code.tutsplus.com/tutorials/using-the-accelerometer-on-android--mobile-22125/**
* Detect a shake based on the ACCELEROMETER sensor
*
* @param event
*/privatevoiddetectShake(SensorEventevent){longnow=System.currentTimeMillis();if((now-mShakeTime)>SHAKE_WAIT_TIME_MS){mShakeTime=now;floatgX=event.values[0]/SensorManager.GRAVITY_EARTH;floatgY=event.values[1]/SensorManager.GRAVITY_EARTH;floatgZ=event.values[2]/SensorManager.GRAVITY_EARTH;// gForce will be close to 1 when there is no movementdoublegForce=Math.sqrt(gX*gX+gY*gY+gZ*gZ);// Change background color if gForce exceeds threshold;// otherwise, reset the colorif(gForce>SHAKE_THRESHOLD){soundAcc.start();}}}/**
* Detect a rotation in on the GYROSCOPE sensor
*
* @param event
*/privatevoiddetectRotation(SensorEventevent){longnow=System.currentTimeMillis();if((now-mRotationTime)>ROTATION_WAIT_TIME_MS){mRotationTime=now;// Change background color if rate of rotation around any// axis and in any direction exceeds threshold;// otherwise, reset the colorif(Math.abs(event.values[0])>ROTATION_THRESHOLD||Math.abs(event.values[1])>ROTATION_THRESHOLD||Math.abs(event.values[2])>ROTATION_THRESHOLD){soundGyro.start();}}}