Search code examples
androidandroid-activitygpslocationlistener

How do I pass data from a background thread to the currently displayed activity?


I have two activities, A and B. Activity A implements a LocationListener that periodically prints out the raw GPS coordinates the screen. Activity B is simply a MapActivity with a MapView. I want to be able to pass the periodically-generated coordinates from A to B, even with A paused in the background and B in the foreground. Turns out that the LocationListener indeed continues to retrieve the coordinates when activity A is paused. With B in focus, then, how do I pass those values over from A? Is startActivity(intent) appropriate for an activity that's already started and in the foreground?


Solution

  • If you need to constantly poll for location updates, consider making a "Sticky" service (but not like androidika). Check out the Remote Messenger Service example. This will allow the activity to "hear" updates on the location from the service.

    In my app, i made a base class that "heard" updates of location and if connectivity was lost. Then, the ui activities inherited from this base class. Thus, i could make two different activities receive updates, but minimize duplicate code.