android - 如何在一次 Activity 中读取 GPS 数据和加速度计数据

标签 android android-sensors android-location

是否可以在一次 Activity 中读取 GPS 数据和加速度计数据? 我想读取数据并放入 TextView 中。 我已经读取了加速度计数据,但是当我在 Activity 中添加此代码时应用程序崩溃

public class MainActivity extends Activity implements SensorEventListener {
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    valuesInitialization();
    sensorsInitialization();

}

private void valuesInitialization()
{
    Config.mContext = this;
    mInitialized = false;
    mLastAccelerometer[0] = INITVALUE;
    mLastAccelerometer[1] = INITVALUE;
    mLastAccelerometer[2] = INITVALUE;
    mLastGyroscope[0] = INITVALUE;
    mLastGyroscope[1] = INITVALUE;
    mLastGyroscope[2] = INITVALUE;
    mLastOrientation[0] = INITVALUE;
    mLastOrientation[1] = INITVALUE;
    mLastOrientation[2] = INITVALUE;
}

private void sensorsInitialization() 
{
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
    mGyroscope = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

    mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
}

private void registListener() 
{
    mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mGyroscope, SensorManager.SENSOR_DELAY_NORMAL);
    mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_NORMAL);
    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
}

protected void onResume() 
{
    super.onResume();
    registListener();
}

protected void onPause() 
{
    super.onPause();
    mSensorManager.unregisterListener(this);
    }
}

有什么办法可以解决这个问题吗?提前致谢

错误日志:enter image description here enter image description here

最佳答案

你必须在你的类中实现一个位置监听器,如下所示

public class LocationListener implements android.location.LocationListener {
    final String LOG_LABEL = "Location Listener>>";

    @Override
    public void onLocationChanged(Location location) {
        Log.d(util.TAG,LOG_LABEL+ "Location Changed");
        if (location != null)
        {
            double longitude = location.getLongitude();
            Log.d(util.TAG,LOG_LABEL+ "Longitude:" + longitude);
            Toast.makeText(getApplicationContext(),"Long::"+longitude,Toast.LENGTH_SHORT).show();
            double latitude = location.getLatitude();
            Toast.makeText(getApplicationContext(),"Lat::"+latitude,Toast.LENGTH_SHORT).show();
            Log.d(util.TAG,LOG_LABEL+ "Latitude:" + latitude);

            cleanUp();

        }
    }

并且还定义了一个位置监听器,如下所示

private android.location.LocationListener locListener;

然后为了让听众开始,做

this.locListener = new LocationListener();

一旦你得到 long/lat 值,不要忘记如下所示进行清理

cleanUp()
{
     // This needs to be done to stop getting the location data and save the
    // battery power.
    if (null != this.locListener && null != locationManager)
    {
        locationManager.removeUpdates(this.locListener);
        this.locListener = null;
    }
}

关于android - 如何在一次 Activity 中读取 GPS 数据和加速度计数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18978271/

相关文章:

java - 如何在 Objectify 上返回自定义对象列表

java - 如何扩展Android Chronometer?

java - 当我尝试从 Activity 类调用函数时崩溃

android - 等效替换已弃用的 Sensor.TYPE_ORIENTATION

android - fusedLocationProviderClient.lastLocation.addOnSuccessListener 始终为空

java - Android Studio Newb - 自动完成或只是安装不正确?

android - 可用的 Android 设备传感器列表

android - 如何以固定频率从 Android 加速度计传感器获取 x、y、z 值,例如每 20 毫秒、40 毫秒或 60 毫秒

java - 如何获取当前位置和下一个当前位置之间的距离

android - 启用gps定位后如何立即获取位置