我有一个安装在汽车内的Android设备。 我需要每秒都收到gps位置更新。 我的问题是我得到了所有必要的数据(卫星数量,坐标,时间等)。
但由于某种原因,“GpsStatus.GPS_EVENT_FIRST_FIX”未被调用或者如果被调用则需要很长时间(如30分钟......)
为什么?这是我的代码:
public class ExtProtocolService extends Service implements GpsStatus.Listener {
private GpsStatus gpsStatus;
private GpsData gpsData;
private LocationManager locationManager;
private LocationListener locationListener;
@Override
public void onCreate() {
locationManager = (LocationManager)
getSystemService(this.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String provider = locationManager.getBestProvider(criteria, true);
if(provider.equals("gps"))
{
locationListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
// not used
}
@Override
// not used
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
// update location parameters when location is changed
@Override
public void onLocationChanged(Location location) {
gpsData.setLatitude(location.getLatitude());
L.m("LATLONG", "latitude " + location.getLatitude());
gpsData.setLongitude(location.getLongitude());
L.m("LATLONG", "longitude " + location.getLongitude());
gpsData.setSpeed(location.getSpeed());
gpsData.setHeading(location.getBearing());
gpsData.setAltitude(location.getAltitude());
gpsData.setHdop(location.getAccuracy());
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 0, locationListener); // update location every 1000 ms
locationManager.addGpsStatusListener(this);
}
}
@Override
public void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_FIRST_FIX:
gpsData.setLocked(true); // gps is locked
break;
case GpsStatus.GPS_EVENT_STOPPED:
gpsData.setLocked(false); // gps unreachable
break;
}
gpsStatus = locationManager.getGpsStatus(null);
// calc number of satelites fixed
Iterable<GpsSatellite> sats = gpsStatus.getSatellites();
int satellites = 0;
int satellitesInFix = 0;
int timetofix = gpsStatus.getTimeToFirstFix();
for (GpsSatellite sat : sats) {
if (sat.usedInFix()) {
satellitesInFix++;
}
satellites++;
}
gpsData.setNumSatellites(satellitesInFix);
L.m("GPS", gpsData.toString());
}
}
编辑:我也无法使用google location API ..