Best way to get current locations from background service


  1. eZACKe

    eZACKe New Member

    I have a Service that I am running in the background every minute. It's an IntentService and I start it like this:

    Code (Text):
    1. Intent i = new Intent(context, GPSTracker.class);
    2.         PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
    3.        
    4.         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    5.        
    6.         if(isOn) {
    7.             alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), POLL_INTERVAL, pi);
    8.         } else {
    9.             alarmManager.cancel(pi);
    10.             pi.cancel();
    11.         }
    That works, my Service is being run every minute (give or take a couple seconds for some reason) whether the app is up, closed, the phone is sleeping, whatever.

    My problem is, I'm not exactly sure how I should be getting the current location from this Service. I'm attempting to use LocationListener and a mix of getLastKnownLocation and hoping onLocationChanged will be called.

    I think my problem is, onLocationChanged is barely getting called. Everytime the service runs and I do getLastKnownLocation I send a message to my database to check, I send a different message everytime onLocationChanged is called. I went to the store and back and getLastKnownLocation was called a few times, but onLocationChanged was only called once and it was incorrect. getLastKnownLocation always seems to be incorrect to.

    This is what I'm doing:


    Code (Text):
    1. protected void onHandleIntent(Intent intent) {
    2.         Log.i(TAG, "In onHandleIntent!");
    3.         Location currentLocation = getLocation(this);
    4.         if(currentLocation == null) {
    5.             saveLocation(-8.0, -8.0, "nothing", "nothing", "nothing");
    6.         } else {
    7.             Geocoder geocoder;
    8.             List<Address> addresses;
    9.             geocoder = new Geocoder(this, Locale.getDefault());
    10.             try {
    11.                 addresses = geocoder.getFromLocation(latitude, longitude, 1);
    12.                 String city = addresses.get(0).getLocality();
    13.                 String country = addresses.get(0).getCountryCode();
    14.                 String state = addresses.get(0).getAdminArea();
    15.                 saveLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), city, state, country);
    16.             } catch (IOException e) {
    17.                 e.printStackTrace();
    18.             }
    19.         }
    20.     }
    Code (Text):
    1. public Location getLocation(Context context) {
    2.         try {
    3.             locationManager = (LocationManager) context
    4.                     .getSystemService(LOCATION_SERVICE);
    5.  
    6.             // getting GPS status
    7.             isGPSEnabled = locationManager
    8.                     .isProviderEnabled(LocationManager.GPS_PROVIDER);
    9.  
    10.             // getting network status
    11.             isNetworkEnabled = locationManager
    12.                     .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    13.  
    14.             if (!isGPSEnabled && !isNetworkEnabled) {
    15.                 // no network provider is enabled
    16.             } else {
    17.                 this.canGetLocation = true;
    18.                 if (isNetworkEnabled) {
    19.                     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
    20.                     Log.d("Network", "Network");
    21.                     if (locationManager != null) {
    22.                         location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    23.                         if (location != null) {
    24.                             latitude = location.getLatitude();
    25.                             longitude = location.getLongitude();
    26.                         }
    27.                     }
    28.                 }
    29.                 // if GPS Enabled get lat/long using GPS Services
    30.                 if (isGPSEnabled) {
    31.                     if (location == null) {
    32.                         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, this);
    33.                         Log.d("GPS Enabled", "GPS Enabled");
    34.                         if (locationManager != null) {
    35.                             location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    36.                             if (location != null) {
    37.                                 latitude = location.getLatitude();
    38.                                 longitude = location.getLongitude();
    39.                             }
    40.                         }
    41.                     }
    42.                 }
    43.             }
    44.  
    45.         } catch (Exception e) {
    46.             e.printStackTrace();
    47.         }
    48.  
    49.         return location;
    50.     }
    Code (Text):
    1.  @Override
    2.     public void onLocationChanged(Location location) {
    3.         if(location == null) {
    4.             saveLocation(-8.0, -8.0, "nothing_listener", "nothing_listener", "nothing");
    5.         } else {
    6.             Geocoder geocoder;
    7.             List<Address> addresses;
    8.             geocoder = new Geocoder(this, Locale.getDefault());
    9.             try {
    10.                 addresses = geocoder.getFromLocation(latitude, longitude, 1);
    11.                 String city = addresses.get(0).getLocality();
    12.                 String country = addresses.get(0).getCountryCode();
    13.                 String state = addresses.get(0).getAdminArea();
    14.                 saveLocation(location.getLatitude(), location.getLongitude(), city +"_listener", state +"_listener", country +"_listener");
    15.             } catch (IOException e) {
    16.                 e.printStackTrace();
    17.             }
    18.         }
    19.     }
    So what am I doing wrong? I'm sure plenty...

    Advertisement
  2. eZACKe

    eZACKe New Member

    Edit: If someone could tell me how to properly insert code I'll fix my first post.

Share This Page