-1

i'm currently setting up an automation on my Android device and i need to know which activity is currently in the foreground so i can include it in the automation as a trigger. I'm looking for the app activity withing the whole package

Thanks

  • Possible duplicate of [Checking if an Android application is running in the background](https://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background) – John Stark Aug 16 '18 at 00:02

1 Answers1

0

Sure, use Activity Manager.

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;
componentInfo.getPackageName();

You will need the following permission on your manifest:

<uses-permission android:name="android.permission.GET_TASKS"/>
Josh
  • 386
  • 5
  • 14