-1

I need to get package name or app name of any app when it gets opened by user. I searched about this but didn't get proper example.

Anyone can give me suggestions for this case? Thank you so much.!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Trung Đoan
  • 643
  • 7
  • 18

1 Answers1

1

You have to create your background service for that , which will continuously monitor top app in device .

So first of all make a service which will start after your app launch .In your service , use this

ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
activityOnTop=ar.topActivity.getClassName();

activityOnTop will give you the current running app in your phone . Now do whatever you want after getting top activity.

Tejas Pandya
  • 3,987
  • 1
  • 26
  • 51
  • I also found the same answer, but I wanted to ask if there was a broadcast or anything similar that I could listen to and when the app was opened it would send me information. Thanks – Trung Đoan Jan 03 '18 at 16:09
  • @TrungĐoan what you want to do actually ? – Tejas Pandya Jan 04 '18 at 05:00
  • I want to know the package name when the user opens any app, As I Know from android L, RunningTaskInfo is deprecated and instead can use RunningAppProcessInfo. But when i using RunningAppProcessInfo in samsing device, it can get only my package name. – Trung Đoan Jan 04 '18 at 07:00
  • @TrungĐoan this is exactly mentioned in the answer . make service , run it in background , put that code in it . and you will get your package name – Tejas Pandya Jan 04 '18 at 07:02
  • I found a component that helped me solve this problem. I use AccessibilityService to detect WINDOW STATE CHANGED and it works well for me. Thank you for your support. – Trung Đoan Jan 04 '18 at 07:11