Android GPS显示旧数据
我有一个活动,请求gps_提供商,如果禁用,则请求网络_提供商。 问题是,当gps传感器启用,但未接收到信号时(例如在家中),他将获取旧数据(位置非空),而不是新位置的网络供应商。我可以清除旧的gps数据吗 代码如下:Android GPS显示旧数据,android,gps,position,Android,Gps,Position,我有一个活动,请求gps_提供商,如果禁用,则请求网络_提供商。 问题是,当gps传感器启用,但未接收到信号时(例如在家中),他将获取旧数据(位置非空),而不是新位置的网络供应商。我可以清除旧的gps数据吗 代码如下: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ..... LocationManager lm = (LocationM
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;
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
.....
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
lm.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,此);
if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
lm.RequestLocationUpdate(LocationManager.NETWORK_提供程序,0,0,this);
Location Location=lm.getLastKnownLocation(LocationManager.GPS\U提供程序);
if(位置==null){
if(lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
location=lm.getLastKnownLocation(LocationManager.NETWORK\u提供程序);
if(位置==null){
showGPSDisabledAlertToUser();
}
}
如果(位置!=null){
此。onLocationChanged(位置);
}
位置更改后的公共无效(位置l){
洛卡特兰德克雷斯(l);
}
私人空间locateLandkreis(位置l){
新下载警告(this).执行(l);
}
私有类DownloadWarn扩展异步任务{
.....
@凌驾
受保护管柱背景(位置…loc){
......
LocationManager lm=(LocationManager)getSystemService(Context.LOCATION\u服务);
lm.removeUpdates(GPSActivity.this);
返回数据;
}
谢谢
Oliver您应该检查从GPSProvider获得的位置的时间,如果时间早于某个特定的阈值,也可以查找网络提供商的位置 因此,与其
if (location == null) { ...
这样做
if (location == null ||
System.currentTimeMillis()-location.getTime() > THRESHOLD) { ...
其中THRESHOLD
将是一个以毫秒为单位的treshold