Take the 2-minute tour ×

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

share|improve this question

3 Answers 3

up vote 37 down vote accepted


You may not own the "currently running Activity".


  1. Send a broadcast Intent to the activity -- here is a sample project demonstrating this pattern
  2. Have the activity supply a PendingIntent (e.g., via createPendingResult()) that the service invokes
  3. 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
  4. 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
share|improve this answer
Thanks, but as I do have a looot of activities, and don't want to update them all, I was looking for an android way to get the foregroundActivity. As you say, I should not be able to do that. Means I have to get my way around this. –  George Oct 7 '10 at 11:36
@George: What you want wouldn't help you, anyway. As you point out, you have "a looot of activities". Hence, those are all separate classes. Hence, there is nothing your service can do with them without either a massive if-block of instanceof checks or refactoring the activities to share a common superclass or interface. And if you're going to refactor the activities, you may as well do it in a way that fits better with the framework and covers more scenarios, such as none of your activities being active. #4 is probably the least work and most flexible. –  CommonsWare Oct 7 '10 at 11:43
Thanks, but I have a better solution. All my activities extends a customised BaseActivity class. I have set a ContextRegister that registeres the activity as current whenever it is on the foreground, with literraly 3 lines in my BaseActivity class. Still, thanks for the support. –  George Oct 8 '10 at 12:33
@George: Watch out for memory leaks. Mutable static data members are to be avoided in Java wherever possible. –  CommonsWare Oct 8 '10 at 13:06
I have tightly encapsualted the object, though I will indeed keep this in mind. Thanks. –  George Oct 8 '10 at 13:22

Here's a good way to do it using the activity manager. You basically get the runningTasks from the activity manager. It will always return the currently active task first. From there you can get the topActivity.

Example here

There's an easy way of getting a list of running tasks from the ActivityManager service. You can request a maximum number of tasks running on the phone, and by default, the currently active task is returned first.

Once you have that you can get a ComponentName object by requesting the topActivity from your list.

Here's an example.

    ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    Log.d("topActivity", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClassName());
    ComponentName componentInfo = taskInfo.get(0).topActivity;

You will need the following permission on your manifest:

<uses-permission android:name="android.permission.GET_TASKS"/>
share|improve this answer
Your answer is 100% correct.Thanxx 4 it.Its better that u post the same answer here –  Shahzad Imam Apr 28 '12 at 10:44
@ArtOfWarfare haven't verified but yes, the number of fragments you have is irrelevant as they are always hosted by a single activity. –  Nelson Ramirez Sep 9 '12 at 0:59
If i need the current activity more frequently, I have to poll very frequently right? Is there a way to get a callback event whenever the foreground activity changes? –  nizam.sp Mar 28 '13 at 20:42
Just so everybody is aware, the docs state this about the getRunningTasks() method. -------- Note: this method is only intended for debugging and presenting task management user interfaces. This should never be used for core logic in an application, such as deciding between different behaviors based on the information found here. Such uses are not supported, and will likely break in the future. For example, if multiple applications can be actively running at the same time, assumptions made about the meaning of the data here for purposes of control flow will be incorrect. ------------ –  Ryan Jul 3 '13 at 21:55
semifake on api 21 As of LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak person information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive. –  sherpya Dec 22 at 21:21

Use an AccessibilityService

You can detect the currently active window by using an AccessibilityService. In the onAccessibilityEvent callback, check for the TYPE_WINDOW_STATE_CHANGED event type to determine when the current window changes.


This is it should work in most Android versions, including Android 5, AKA Lollipop (API 21). In contrast, the getRunningTasks method isn't intended for this use, and according to the documentation, it will only provide a subset of the actual running tasks in Android 5.

Also, this technique doesn't require polling.


A disadvantage of this is users must enable the service themselves via Android's accessibility settings in order for the AccessibilityEvents to be received.



public class WindowChangeDetectingService
        extends AccessibilityService {

    public void onAccessibilityEvent(AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED)
                "Window Package: " + event.getPackageName()

    public void onInterrupt() {}


Merge this into your manifest:

            <action android:name="android.accessibilityservice.AccessibilityService"/>

Service Info

Put this in res/xml/accessibilityservice.xml:


Ensure you create your.app.ServiceSettingsActivity. So you can provide a UI to users when the view the accessibility service in the Android settings.

share|improve this answer

Your Answer


