实现陀螺仪和加速度计?

时间:2014-01-03 19:43:52

标签: java android

我想获得手机中陀螺仪和加速度计的值。 以下是我的代码

public class MainActivity extends Activity implements  SensorEventListener{

    private SensorManager sensorManager;
    private long lastUpdate;
    private GPSTracker gpss;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        lastUpdate = System.currentTimeMillis();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER && event.sensor.getType() == Sensor.TYPE_GRAVITY) {
              getAccelerometer(event);
            }
    }


    private void getAccelerometer(SensorEvent event) {
        // TODO Auto-generated method stub
        float[] values = event.values;
        // Movement
        float x = values[0];
        float y = values[1];
        float z = values[2];

        float accelationSquareRoot = (x * x + y * y + z * z)
            / (SensorManager.GRAVITY_EARTH * SensorManager.GRAVITY_EARTH);
        long actualTime = System.currentTimeMillis();
        if (accelationSquareRoot >= 10) //
        {
          if (actualTime - lastUpdate < 200) {
            return;
          }
          lastUpdate = actualTime;
          gpss = new GPSTracker(MainActivity.this);
           if(gpss.canGetLocation()){

              double latitude = gpss.getLatitude();
              double longitude = gpss.getLongitude();

              // \n is for new line
              Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    
          }else{
              // can't get location
              // GPS or Network is not enabled
              // Ask user to enable GPS/network in settings
              gpss.showSettingsAlert();
          }
        }

    }

    @Override
      protected void onResume() {
        super.onResume();
        // register this class as a listener for the orientation and
        // accelerometer sensors
        Sensor gsensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
        Sensor asensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(this,
            gsensor,
            SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this,
                asensor,
                SensorManager.SENSOR_DELAY_NORMAL);
      }

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


}

如果我使用

,代码可以正常工作
@Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                  getAccelerometer(event);
                }
        }

但是当我包含* TYPE_GRAVITY *时,它不起作用。我想我在 getType()函数中犯了错误。有人能告诉我同时检测两个事件的正确语法吗?

1 个答案:

答案 0 :(得分:2)

在您的代码中:

@Override
public void onSensorChanged(SensorEvent event) {
    // TODO Auto-generated method stub
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER 
        && event.sensor.getType() == Sensor.TYPE_GRAVITY) {
        getAccelerometer(event);
        }
}

您正在获取事件类型的值,并询问它是否同时出现两个不同的事情。没有什么不同于:

int a = 1;
if (a == 1 && a == 2)  { ... }

每个SensorEvent都只有一种类型。如果您想在任何一种情况下调用您的方法,请在条件中使用||代替&&

如果您想独立行动,则需要进行两次检查。 switch()是一个很好的方式:

@Override
public void onSensorChanged(SensorEvent event) {

    switch(event.sensor.getType()) {

        case Sensor.TYPE_ACCELEROMETER:
            getAccelerometer(event);
            break;
        case Sensor.TYPE_GRAVITY:
            doSomethingDifferent();
            break;
        }
    }
}