GPS 不更新
大家晚上好,
我目前正在开发一个应用程序,它可以获取不同传感器的值,例如加速度计、接近传感器、指南针和 GPS,这些值我将用于我的机器人项目。然而,我的 GPS 存在问题,当我在不关闭活动的情况下更改位置时,GPS 不会更新。事实上,如果我停止应用程序然后在另一个地方重新启动它,坐标将是正确的,所以这确实是一个更新问题。另请注意,当屏幕改变方向(横向 -> 纵向)时,会重新计算位置。但我希望它能在不接触任何东西的情况下工作,呵呵。
这是我的代码:
package com.pIndus.sensors;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class Sensors extends Activity implements SensorEventListener{
SensorManager sm;
LocationManager lm;
LocationListener ls;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean accelSupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST);
if(!accelSupported)
{
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
((TextView)findViewById(R.id.acc)).setText("Accéléromètre non disponible");
}
boolean compassSupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
if(!compassSupported)
{
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION));
((TextView)findViewById(R.id.compass)).setText("Boussole non disponible");
}
boolean proxySupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY),SensorManager.SENSOR_DELAY_FASTEST);
if(!proxySupported)
{
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY));
((TextView)findViewById(R.id.proxy)).setText("Capteur de proximité non disponible");
}
ls = new MyLocationListener();
lm.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, ls);
}
@Override
public void onStop()
{
super.onStop();
lm.removeUpdates(ls);
sm.unregisterListener(this);
}
@Override
public void onPause()
{
super.onStop();
lm.removeUpdates(ls);
sm.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event)
{
switch(event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
onAccelChanged(event);
break;
case Sensor.TYPE_ORIENTATION:
onCompassChanged(event);
break;
case Sensor.TYPE_PROXIMITY:
onProxyChanged(event);
break;
}
}
public void onAccelChanged(SensorEvent event)
{
float aX,aY,aZ;
aX = event.values[0];
aY = event.values[1];
aZ = event.values[2];
((TextView)findViewById(R.id.axeX)).setText("Axe X : " + aX);
((TextView)findViewById(R.id.axeY)).setText("Axe Y : " + aY);
((TextView)findViewById(R.id.axeZ)).setText("Axe Z : " + aZ);
}
public void onCompassChanged(SensorEvent event)
{
float azimuth,pitch,roll;
azimuth = event.values[0];
pitch = event.values[1];
roll = event.values[2];
((TextView)findViewById(R.id.azimuth)).setText("Azimuth : " + azimuth);
((TextView)findViewById(R.id.pitch)).setText("Pitch : " + pitch);
((TextView)findViewById(R.id.roll)).setText("Roll : " + roll);
}
public void onProxyChanged(SensorEvent event)
{
float x;
x = event.values[0];
((TextView)findViewById(R.id.prox)).setText("Proximité : " + x);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
double longitude, lattitude;
longitude = location.getLongitude();
lattitude = location.getLatitude();
((TextView)findViewById(R.id.longi)).setText("Longitude : " + longitude);
((TextView)findViewById(R.id.latti)).setText("Lattitude : " + lattitude);
lm.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, ls);
}
@Override
public void onProviderDisabled(String provider) {
((TextView)findViewById(R.id.warnGPS)).setText("GPS Desactivé");
}
@Override
public void onProviderEnabled(String provider) {
((TextView)findViewById(R.id.warnGPS)).setText("GPS Activé");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
有人知道我的问题吗?
非常感谢您的帮助,祝您度过愉快的一天/晚上/夜晚。
Good evening everyone,
I'm currently doing an application which get the values of different sensors like the accelerometer, the proximity sensor, the compass and the GPS, values that I'm going to use for my robotics project. Yet, I have a problem with the GPS which doesn't update when I change location without closing the Activity. Indeed, if I stop the application and then restart it at another place, the coords will be correct, so that's really a problem of updating. Please notice too that the location is being recalculated when the screen change its orientation (landscape -> portrait). But I would like it to work without touching anything, hehe.
So here is my code :
package com.pIndus.sensors;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class Sensors extends Activity implements SensorEventListener{
SensorManager sm;
LocationManager lm;
LocationListener ls;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sm = (SensorManager) getSystemService(SENSOR_SERVICE);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
boolean accelSupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_FASTEST);
if(!accelSupported)
{
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER));
((TextView)findViewById(R.id.acc)).setText("Accéléromètre non disponible");
}
boolean compassSupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST);
if(!compassSupported)
{
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION));
((TextView)findViewById(R.id.compass)).setText("Boussole non disponible");
}
boolean proxySupported = sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY),SensorManager.SENSOR_DELAY_FASTEST);
if(!proxySupported)
{
sm.unregisterListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY));
((TextView)findViewById(R.id.proxy)).setText("Capteur de proximité non disponible");
}
ls = new MyLocationListener();
lm.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, ls);
}
@Override
public void onStop()
{
super.onStop();
lm.removeUpdates(ls);
sm.unregisterListener(this);
}
@Override
public void onPause()
{
super.onStop();
lm.removeUpdates(ls);
sm.unregisterListener(this);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event)
{
switch(event.sensor.getType())
{
case Sensor.TYPE_ACCELEROMETER:
onAccelChanged(event);
break;
case Sensor.TYPE_ORIENTATION:
onCompassChanged(event);
break;
case Sensor.TYPE_PROXIMITY:
onProxyChanged(event);
break;
}
}
public void onAccelChanged(SensorEvent event)
{
float aX,aY,aZ;
aX = event.values[0];
aY = event.values[1];
aZ = event.values[2];
((TextView)findViewById(R.id.axeX)).setText("Axe X : " + aX);
((TextView)findViewById(R.id.axeY)).setText("Axe Y : " + aY);
((TextView)findViewById(R.id.axeZ)).setText("Axe Z : " + aZ);
}
public void onCompassChanged(SensorEvent event)
{
float azimuth,pitch,roll;
azimuth = event.values[0];
pitch = event.values[1];
roll = event.values[2];
((TextView)findViewById(R.id.azimuth)).setText("Azimuth : " + azimuth);
((TextView)findViewById(R.id.pitch)).setText("Pitch : " + pitch);
((TextView)findViewById(R.id.roll)).setText("Roll : " + roll);
}
public void onProxyChanged(SensorEvent event)
{
float x;
x = event.values[0];
((TextView)findViewById(R.id.prox)).setText("Proximité : " + x);
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
double longitude, lattitude;
longitude = location.getLongitude();
lattitude = location.getLatitude();
((TextView)findViewById(R.id.longi)).setText("Longitude : " + longitude);
((TextView)findViewById(R.id.latti)).setText("Lattitude : " + lattitude);
lm.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, ls);
}
@Override
public void onProviderDisabled(String provider) {
((TextView)findViewById(R.id.warnGPS)).setText("GPS Desactivé");
}
@Override
public void onProviderEnabled(String provider) {
((TextView)findViewById(R.id.warnGPS)).setText("GPS Activé");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
Has anyone an idea about my problem ?
Thank you very much for your help and have a nice day/evening/night.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将以下行替换为
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 1.0f, ls);
Replace below line by
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 1.0f, ls);