1: private float x, y, z;
2: @Override
3: protected void onCreate(Bundle savedInstanceState) {
4: super.onCreate(savedInstanceState);
5: sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
6: Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
7: SensorEventListener lsn = new SensorEventListener() {
8: public void onSensorChanged(SensorEvent e) {
9: x = e.values[SensorManager.DATA_X];
10: y = e.values[SensorManager.DATA_Y];
11: z = e.values[SensorManager.DATA_Z];
12: setTitle("x="+(int)x+","+"y="+(int)y+","+"z="+(int)z);
13: }
14:
15: public void onAccuracyChanged(Sensor s, int accuracy) {
16: }
17: };
18: //注册listener,第三个参数是检测的精确度
19: sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
20: }
将x、y、z的坐标显示出来。