0

While the activity is on, I go to the background and when I want to learn the classname from within a service, I see "com.miui.home.launcher.Launcher". I need the activity class. Where am I going wrong?

The codes I wrote for classname are as follows;

    ActivityManager am = (ActivityManager) 
    getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
    ComponentName componentInfo = taskInfo.get(0).topActivity;
    String className = componentInfo.getClassName();
ay.meltem
  • 3
  • 3
  • are you shure that `com.miui.home.launcher.Launcher` isn't `Activity`? can you elaborate more what do you want to achieve? – snachmsm Feb 01 '21 at 08:26
  • @snachmsm Of course, when I am in UserActivity, I put the application in the background and I want to get the class name using the above codes to find out what activity I am in from a service that continues to run. But when it is in the background componentInfo comes as "com.miui.home.launcher.Launcher". However, if it is not in the background, I can see the class name as UserActivity. – ay.meltem Feb 01 '21 at 08:33

1 Answers1

0

com.miumi.home.launcher.Launcher is Activity set as devices launcher - "desktop" app responsible for showing all your apps grid, widgets etc. when your app is in background (put there by e.g. Home button) and any of your Activites isn't present on the screen then your device is showing Launcher app and this is reporting your code

note that getRunningTasks method is deprecated and won't let you know which third-party app is on foreground. currently this method may only return info about your app or Launcher/"desktop"

https://developer.android.com/reference/android/app/ActivityManager#getRunningTasks(int)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Thanks for your answer. So is there no way I can see the activity name before going into the background while in the background? – ay.meltem Feb 01 '21 at 08:59
  • I used this List task = manager.getRunningAppProcesses(); // Get the info we need for comparison. ComponentName componentInfo2 = task.get(0).importanceReasonComponent; but not working – ay.meltem Feb 01 '21 at 09:06
  • it would be a bit insecure, would reveal users behavior - which app and how long was used. I'm not surpised that this isn't an option for usueal developer nowadays – snachmsm Feb 01 '21 at 11:42