0

I'm working on an android project. I need to receive other applications' usage time. I simply just need how many application is running and what are they. How can I access these kinds of data?

I found some articles about receiving another's SQLite DB but I really don't need this.

2 Answers2

1

You may follow the steps:

  • Retrieve the list of the tasks that are currently running by calling getRunningTasks()
  • Go through each of the task and retrieve the PackageName
  • Get AppName from the PackagName
  • Add the AppName in a list

Here is the implementation:

private List<String> getRunningAppList()
{
    Set<String> appNameList = new HashSet<>();

    PackageManager pm = getPackageManager();
    ActivityManager aManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

    // Get the list of the tasks that are currently running
    List<ActivityManager.RunningTaskInfo> infoList = aManager.getRunningTasks(Integer.MAX_VALUE);

    for (ActivityManager.RunningTaskInfo taskInfo : infoList)
    {
        // Get the packageNAme
        String pkgName = taskInfo.topActivity.getPackageName();
        String appName = null;
        // Get the appName
        try
        {
            appName = pm.getApplicationLabel(pm.getApplicationInfo(pkgName,PackageManager.GET_META_DATA)).toString();
        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }

        // Adding the appName
        appNameList.add(appName);
    }

    return new ArrayList<>(appNameList);
}
Md. Sabbir Ahmed
  • 850
  • 8
  • 22
  • Sabbir, i don't know why you did not respect my opinion. getRunningTasks is deprecated mate. I already checked your comment but it is not useful. – arthurealike Mar 26 '19 at 10:46
  • I am extremely sorry. Your comment "Yes but this code only shows my running app information, i need names of running applications on device currently." did not mean that. I know that getRunningTasks() is deprecated but it will work, I think. However, you may use getRunningAppProcesses() method instead of getRunningTasks(Integer.MAX_VALUE). – Md. Sabbir Ahmed Mar 26 '19 at 11:00
  • As i said both getRunningTasks() and getRunningAppProcesses() are same. They show only my application's name. Just respect my solution bro. – arthurealike Mar 26 '19 at 12:04
1

Using UsageStats, UsageStatsManager is simply the best approach for this topic.

https://developer.android.com/reference/android/app/usage/UsageStats

UsageStats.getLastTimeUsed() informs you about a specific application's last usage time by date format.

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182