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)?
Related problems
- Enable and disable a Broadcast Receiver
- How to get current foreground activity context in android?
- getRunningTasks doesn't work in Android L
- accessibility service is not started
- How can I programmatically start and stop an AccessibilityService?
- Is it possible to use adb commands to click on a view by finding its ID?
- Restart Accessibility Service After Crash