Issue
Is there a native android way to get a reference to the currently running Activity from a service?
I have a service running on the background, and I would like to update my current Activity when an event occurs (in the service). Is there a easy way to do that (like the one I suggested above)?
Solution
Is there a native android way to get a reference to the currently running Activity from a service?
You may not own the "currently running Activity".
I have a service running on the background, and I would like to update my current Activity when an event occurs (in the service). Is there a easy way to do that (like the one I suggested above)?
- Send a broadcast
Intent
to the activity -- here is a sample project demonstrating this pattern - Have the activity supply a
PendingIntent
(e.g., viacreatePendingResult()
) that the service invokes - Have the activity register a callback or listener object with the service via
bindService()
, and have the service call an event method on that callback/listener object - Send an ordered broadcast
Intent
to the activity, with a low-priorityBroadcastReceiver
as backup (to raise aNotification
if the activity is not on-screen) -- here is a blog post with more on this pattern
Answered By - CommonsWare
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.