开发者

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

开发者 https://www.devze.com 2023-04-02 09:09 出处:网络
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

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. T开发者_StackOverflow社区urns 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?


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.


I d' recommend using a service, that runs in the background. Or rather than having one activity decide when to update the location, write an IntentService that you launch from either of the 2 activities whenever you need them. Take a look at PlacesUpdateService This does more than just look for the gps coordiantes, but it will give you the idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号