启动服务后:onLocationChanged不会被调用,甚至请求更新
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, LocationService.class);
pi = PendingIntent.getService(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
am.cancel(pi);
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(), 30*1000, pi);
服务:
public class LocationService extends Service implements LocationListener {
public static double curLat = 0.0;
public static double curLng = 0.0;
private LocationManager mgr;
private String best;
private Location location;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
best = LocationManager.NETWORK_PROVIDER;
location = mgr.getLastKnownLocation(best);
if (location != null) {
dumpLocation(location);
mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
5 * 1000, 0, this);
}
return START_NOT_STICKY;
}
}
private void dumpLocation(Location l) {
SimpleDateFormat s = new SimpleDateFormat("dd/MM/yyyy:hh:mm:ss",
Locale.ENGLISH);
String format = s.format(l.getTime());
//The above time is always 21/03/2013:09:53:41 which is more than a week old
curLat = l.getLatitude();
curLng = l.getLongitude();
}
@Override
public void onLocationChanged(Location location) {
//This never seems to be called
dumpLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
每30秒的服务被调用,但requestLocationUpdates
似乎从来没有致电onLocationChanged
。当我检查了一周前lastKnownLocation
的时间。
像foursquare,本地,地图等应用程序给出当前位置禁用GPS。
我错过了什么?
在旁注:位置更新可以返回空位置。一定要处理它... – Warpzit
你在设备或模拟器中测试?你的GPS是否启用?你是否像露天环境一样在外面进行测试? – Lucifer
@Warpzit,是的。但我正在获得一个位置。我转储并获取时间戳。 –