gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:20:23 25 4
gpt4 key购买 nike

是否可以在一次 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com