3

What I want is that I need the reference to the current Activity of ANY application that is on the foreground.

Basically I have written a service and I want to get the reference to the "current" foreground activity in that service. With "current" I mean any application activity other than the one that has started that service.

Adil Sarwar
  • 122
  • 1
  • 10

2 Answers2

0

This code is working for me:

ActivityManager manager = (ActivityManager)mContext.getSystemService(Context.ACTIVITY_SERVICE);    
List<ActivityManager.RunningAppProcessInfo> tasks = manager.getRunningAppProcesses();
Log.d("App", tasks.get(0).processName);
jlopez
  • 6,287
  • 2
  • 51
  • 90
  • This give the access to only the information about the current process. But I need the reference to the activity that is currently in the foreground. – Adil Sarwar Jun 10 '15 at 15:43
  • Yeah. Like the @CommonsWare said its not possible what I want. I agree with him. Thanks for your answer though! – Adil Sarwar Jun 10 '15 at 16:20
0

What I want is that I need the reference to the current Activity of ANY application that is on the foreground.

That is not possible. Other applications are running in other processes; you do not have access to Java objects, such as Activity instances, in those processes.

CommonsWare
  • 954,112
  • 185
  • 2,315
  • 2,367