Android: Is there a way to obtain altitude aside from Location.getAltitude()? (I've heard using Sensor)

十年热恋 提交于 2019-12-18 07:10:39

问题


Is there a way to obtain altitude aside from Location.getAltitude()? I've heard using Sensor but don't have idea and can't find on net.

UPDATES1 I found SensorManager.getAltitude(float, float) using API Level 9 up but don't have idea on how to implement it. Would you share with us.


回答1:


I know it's late but for anyone looking for it, that might help

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;

public class MyActivity extends Activity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor sensor;
    private float altitude = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (sensor != null) 
            sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

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

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {}

    @Override
    public void onSensorChanged(SensorEvent event) {
        float presure = event.values[0];
        altitude = SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, presure);

        System.out.println("altitude => " + altitude);
    }
}


来源:https://stackoverflow.com/questions/7410095/android-is-there-a-way-to-obtain-altitude-aside-from-location-getaltitude-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!