2011-12-20 164 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

使用計時器並運行線程獲取每一分鐘的當前位置。

相關問題