• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps get data from BroadcastReceiver called via requestLocationUpdates

markrbray

Lurker
Hi, I'm new to Android and I'm having the following problem. I'm writing a sample application where I have an intent service that first checks all location providers to get the last known location. If none of the last known locations provides an accurate (or timely) enough location then the location manager's requestLocationUpdates method is called with a BroadcastReceiver intent. Each time the broadcast receiver's onReceive method is called it should check the location to see if it is accurate and/or timely enough. I also have a TimerTask in the intent service that eventually goes off and should check to see if an accurate and/or timely enough location update has been obtained. The problem I'm having is that I don't know how to get the location data obtained in the broadcast receiver back to the intent service. Seems like this should be an easy thing to do but I've been agonizing over this for days. The only way I can think to do it is to write the data to an SQLite db in the broadcast receiver and then read those records back in the intent service, but this seems unnecessarily complicated. Does anyone know what the right way is to get the data back to the intent service? Should I even be using a broadcast receiver for requestLocationUpdates? Is there an easier way to do this altogether? Here is the code:

Code:
public class GetLocationService extends IntentService
{
  public GetLocationService()
  {
    super("something");
  }
  LocationManager  locationManager;

  
  long             maxFixLateness;
  float            maxFixPosUncertainty;

 
  boolean          usableLocObtained;
  Location         bestLoc      = null;
  float            bestLocScore = 0;


  Intent locChangeI;
  PendingIntent pLocChangeI;

  @Override
  final protected void onHandleIntent(Intent intent)
  {
    maxFixLateness = 30000;
    maxFixPosUncertainty = 30;

   
    long curTime = System.currentTimeMillis();
    
    
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);


    // Check for a usable location fix
    List<string> matchingProviders = locationManager.getAllProviders();
    for (String provider : matchingProviders)
    {
      Location location = locationManager.getLastKnownLocation(provider);
      if (location != null)
      {
        
          //...some code to check if the location is accurate or timely enough

      }
    }
    
    

    if (bestLoc == null)
    {
      locChangeI = new Intent(this, HandleLocationUpdateReceiver.class);
      pLocChangeI = PendingIntent.getBroadcast(this, 0, locChangeI, PendingIntent.FLAG_UPDATE_CURRENT);

      usableLocObtained = false;
      locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, pLocChangeI);
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, pLocChangeI);

      // Call the timer that will periodically check to see if a usable location
      // has been obtained.
      new LocFixCheckTimer(60000, 30, 1000);
    }
  } 
  

  private class LocFixCheckTimer
  {
    Timer timer;
    long  numChecks;

    minInitSearchTime,
    public LocFixCheckTimer(long initSearchTime, long maxRechecks, long recheckFreq)
    {
      numChecks = maxRechecks;
      timer = new Timer();
      // Wait 2 seconds before checking for a
      // fix again
      timer.schedule(new CheckLocTask(), initSearchTime, recheckFreq);
    }

    class CheckLocTask extends TimerTask
    {

      public void run()
      {
        if (numChecks &gt; 0)
        {
          if (usableLocObtained == true)
          {
           
             // I want to use the location data obtained from the 
             // HandleLocationUpdateReceiver's onReceive method
             // but I don't how to get that data here.

          }
        }
        else
        {
          // Cancel the timer. We've timed-out on searching
          // for a usable location fix
          timer.cancel();
        }
        --numChecks;
      }
    }
  }

}

Here's the broadcast receiver:

Code:
public class HandleLocationUpdateReceiver extends BroadcastReceiver
{ 
  @Override
  public void onReceive(Context context, Intent intent)
  {
    Location loc = (Location) intent.getExtras().get(LocationManager.KEY_LOCATION_CHANGED);
    if (loc != null)
    {
      double lat = loc.getLatitude();
      double lon = loc.getLongitude();

      // Do some checking to see how accurate and timely the location is
      // here and somehow get it back to the intent service.
    }
  }
}

Any help would be greatly appreciated. Thanks!</string>
 
Back
Top Bottom