android的Accelerometer值z不变

时间:2014-10-23 01:53:02

标签: android accelerometer

这是我手机的一个屏幕截图:

this is one screen shot of my phone


public class MainActivity extends Activity implements SensorEventListener {

    private SensorManager sensorManager;
    private TextView tvSensor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvSensor = (TextView) findViewById(R.id.tvMotionSensor);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }
    @Override
    public void onSensorChanged(SensorEvent event) {
        switch (event.sensor.getType()) {
        case Sensor.TYPE_ACCELEROMETER:
            //Z axis`s value do not change 
            String accelerometer ="x: "+ event.values[0]+"\n" +"y: " +event.values[1]+"\n"+"z: "+event.values[2]+"\n";
            tvSensor.setText(accelerometer);
            Log.d("z", String.valueOf(event.values[2]));
            break;
        case Sensor.TYPE_GRAVITY:
            break;
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_UI);
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),SensorManager.SENSOR_DELAY_FASTEST );

    }
    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);

    }

作为代码,我的手机显示x,y约为0.01,z的值为19.614且不会改变。

0 个答案:

没有答案