如何使用SensorListener获取或不使用设备?

时间:2011-12-05 11:16:53

标签: android android-sensors

  

可能重复:
  Android: I want to shake it!
  How to use Accelerometer to measure distance for Android Application Development
  How to Implement SensorListener?

如果此时设备没有振动,则显示Alertdialog,以便如何让设备振动?

1 个答案:

答案 0 :(得分:1)

使用此代码检查设备是否摇晃

//需要实现SensorListener

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 static final int SHAKE_THRESHOLD = 800;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        ...... // other initializations
    // start motion detection
    sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
    boolean accelSupported = sensorMgr.registerListener(this,
        SensorManager.SENSOR_ACCELEROMETER,
        SensorManager.SENSOR_DELAY_GAME);

    if (!accelSupported) {
        // on accelerometer on this device
        sensorMgr.unregisterListener(this,
                SensorManager.SENSOR_ACCELEROMETER);
    }
    }

    protected void onPause() {
    if (sensorMgr != null) {
        sensorMgr.unregisterListener(this,
                SensorManager.SENSOR_ACCELEROMETER);
        sensorMgr = null;
        }
    super.onPause();
    }

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

    public void onSensorChanged(int sensor, float[] values) {
    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];

        float speed = Math.abs(x+y+z - last_x - last_y - last_z)
                              / diffTime * 10000;
        if (speed > SHAKE_THRESHOLD) {
            // yes, this is a shake action! Do something about it!
                // if device is shake with threshold then this condition become true..
                // you can put your code here... 
        }
        last_x = x;
        last_y = y;
        last_z = z;
        }
    }
    }
}