Android 重力感应 测试代码
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的坐标显示出来。
作者:GangWang
出处:http://www.cnblogs.com/GnagWang/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。