是否可以在一项活动中读取 GPS 数据和加速度计数据?我想读取数据并放入文本视图。我已经读取了加速度计数据,但是当我在 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);
}
}
有没有办法解决这个问题?提前致谢
错误日志: