Android: How can I get the current foreground activity (from a service)?

android, android-activity, service

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., via `createPendingResult()`) 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-priority `BroadcastReceiver` as backup (to raise a `Notification` if the activity is not on-screen) -- here is a blog post with more on this pattern

Problem

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)?

Original source

Related problems