2011-12-20 110 views
0

我想使用后台服务每1分钟或每1km更改获取当前位置。所以我写了一个服务,我的代码服务的调用onStart()方法中is`使用后台服务获取每1分钟或每1km更改的当前位置使用后台服务

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

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

相关问题