我想使用后台服务每1分钟或每1公里更改获取当前位置。所以我编写了一个服务,我的代码在onStart()服务方法中是`
if(lm==null)
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}
catch(Exception ex){
Log.d("location","Exception gps enabled....."+ex.toString());
}
try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}
catch(Exception ex){
Log.d("location","Exception network enabled....."+ex.toString());
}
if(gps_enabled){
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1000,locationListenerGps);
}
if(network_enabled)
{
Log.d("location","network_enabled.....");
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 1000, locationListenerNetwork);
}
}
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
Log.d("location","Latitude from gps....."+ location.getLatitude());
Log.d("location","Longitude from gps....."+location.getLongitude());
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
Log.d("location","Latitude from network ....."+ location.getLatitude());
Log.d("location","Longitude from network ....."+location.getLongitude());
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};code here
现在,当我打电话给这项服务时,我只获得一次位置。问题是什么 ?请帮我解决这个问题
答案 0 :(得分:0)
使用计时器并运行线程以获取每分钟的当前位置。