0

我想使用后台服务每 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

现在,当我调用此服务时,我只会获取一次位置。问题是什么 ?请帮我解决这个问题

4

1 回答 1

0

使用计时器并运行线程以每一分钟获取当前位置。

于 2011-12-20T07:13:53.583 回答