Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 13th, 2012  |  syntax: None  |  size: 2.23 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Get current location for every 1 minute or every 1km change using background service
  2. if(lm==null)
  3.  lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  4.  
  5.             //exceptions will be thrown if provider is not permitted.
  6.             try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}
  7.             catch(Exception ex){
  8.                 Log.d("location","Exception gps enabled....."+ex.toString());
  9.             }
  10.             try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}
  11.             catch(Exception ex){
  12.                 Log.d("location","Exception network enabled....."+ex.toString());
  13.             }
  14.  
  15.  
  16.  
  17.             if(gps_enabled){
  18.  
  19.                 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 6000, 1000,locationListenerGps);
  20.             }
  21.             if(network_enabled)
  22.             {
  23.                 Log.d("location","network_enabled.....");
  24.                 lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 1000, locationListenerNetwork);
  25.  
  26.             }
  27.  
  28.  
  29.     }
  30.     LocationListener locationListenerGps = new LocationListener() {
  31.         public void onLocationChanged(Location location) {
  32.  
  33.             lm.removeUpdates(this);
  34.             lm.removeUpdates(locationListenerNetwork);
  35.             Log.d("location","Latitude from gps....."+ location.getLatitude());
  36.             Log.d("location","Longitude from gps....."+location.getLongitude());
  37.         }
  38.         public void onProviderDisabled(String provider) {}
  39.         public void onProviderEnabled(String provider) {}
  40.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  41.     };
  42.  
  43.     LocationListener locationListenerNetwork = new LocationListener() {
  44.         public void onLocationChanged(Location location) {
  45.  
  46.             lm.removeUpdates(this);
  47.             lm.removeUpdates(locationListenerGps);
  48.             Log.d("location","Latitude from network ....."+ location.getLatitude());
  49.             Log.d("location","Longitude from network ....."+location.getLongitude());
  50.         }
  51.         public void onProviderDisabled(String provider) {}
  52.         public void onProviderEnabled(String provider) {}
  53.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  54.     };code here