Home » Android » java – Detecting android Activity launches

java – Detecting android Activity launches

Posted by: admin May 14, 2020 Leave a comment


I have searched the stackoverflow for this question and found :
Android, Detect when other apps are launched and
Reliable way to detect application launch from home / desktop? , which kind of answers my question but it’s still not enough.

What I want to do
I want to create a widget/app that shows the user of the device a list of recent apps and a list of most used apps.

How can I have accurate data about the apps in order to build a list of most used apps.

Base information

  • I am compiling my own Android OS (4.2 based) code so I have access to
  • I am developing the launcher as well.
  • It needs to pass the CTS and be Google approved.
  • I can make the app with system privileges.
  • I know that this might be a security issue for Google.

Some solutions

The recent apps can be found via ActityManager, getRecentApps method, so now problem there.

I have searched the web for this and already found the following solutions:

  1. Use a service to query the activityManager, getRunningTasks method every X seconds and build the list ( innacurate information, also using a lot of batery ).
  2. Use the logcat to get this information (seems like a hack to me, needs system permissions)
  3. Change the activityManager itself in order to provide this information (will most likely fail the CTS tests)
  4. Use the launcher to verify the apps that were launched (misses the apps launched inside other apps)

Anything else I have missed?

Thanks in advance,
Tiago Costa

How to&Answers:

Android default recent apps dialog implementation is best reference for you, look here..check reloadButtons()

final ActivityManager am = (ActivityManager)
        final List<ActivityManager.RecentTaskInfo> recentTasks =
                am.getRecentTasks(MAX_RECENT_TASKS, ActivityManager.RECENT_IGNORE_UNAVAILABLE);

You can try in similar way..