是否有可能在一项活动中读取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);
}
}
有什么方法可以解决这个问题吗?提前致谢
错误日志:
答案 0 :(得分:2)
您是否在Manifest.xml中添加了GPS权限?
答案 1 :(得分:2)
您必须在班级中实施位置监听器,如下所示
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;
}
}
答案 2 :(得分:1)
registListener
(拼写错误):
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);
}
发生错误是因为mLocationListener为空。
您应该在自己的活动中实施LocationListener
,并在mLocationManager.requestLocationUpdates
传递this
作为听众。
编辑:如果您实施位于LocationListener
的{{1}},您可以覆盖这些方法:
android.location
当用户移动距离等于或高于public void onLocationChanged (Location location);
public void onProviderEnabled (String provider);
public void onProviderDisabled (String provider);
public void onStatusChanged (String provider, int status, Bundle extras)
的第三个参数中指定的距离时,并且当经过的时间超过第二个参数中指定的时间时,调用{p> onLocationChanged
,毫秒。您可以根据需要使用mLocationManager.requestLocationUpdates
对象。
当启用提供GPS位置的提供商之一时,将调用 location
。您可以检索提供商的类型(GPS可能为onProviderEnabled
,Wi-Fi或手机位置可能为LocationManager.GPS_PROVIDER
。)
LocationManager.NETWORK_PROVIDER
与onProviderDisabled
类似,但在提供商已停用时会调用它。
最后,包含onProviderEnabled
的所有方法都记录在here
onStatusChanged
类位于包Location
中,可提供当前用户在高度,方位,速度,时间,演员,纬度,经度,位置提供商和准确度方面的位置。