0

I am attaching one layout to window manager from my android widget(which floats on all the screens). Now I want to hide that layout from window manager if any app like (browser, settings etc) opened. How to get to know that other app(like browser) got launched ?

Raghav
  • 93
  • 1
  • 7

1 Answers1

0

You can get the top App's package name using:

Context context = someArbitraryContext;
ActivityManager am = (ActivityManager) context.
    getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();

You'll also need the <uses-permission android:name="android.permission.GET_TASKS" /> permission.

Once you have the package name, you can use the PackageManager to figure out which app it is.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195