我有一个活动,请求 gps_provider,如果禁用,则请求 network_provider。问题是,当 gps 传感器启用,但没有接收到信号(例如在房子里)时,他将获取旧数据(位置不为空)而不是新位置的 network_provider。我可以清除旧的 gps 数据吗?
这里的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location == null) {
showGPSDisabledAlertToUser();
}
}
if (location != null) {
this.onLocationChanged(location);
}
public void onLocationChanged(Location l) {
locateLandkreis(l);
}
private void locateLandkreis(Location l) {
new DownloadWarn(this).execute(l);
}
private class DownloadWarn extends AsyncTask<Location, Integer, String> {
.....
@Override
protected String doInBackground(Location... loc) {
......
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.removeUpdates(GPSActivity.this);
return data;
}
谢谢奥利弗