Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure Time Spent on Android Applications

Tags:

android

I am new to android. In my application, i want to track for how much time other applications (which are installed on device) are used (in foreground).

Is it possible? If yes then how?

Thanks in advance!

like image 977
Unnati Avatar asked May 06 '13 11:05

Unnati


People also ask

Can you see how much time you've spent on an app?

Tap "Show your data" in the Your Digital Wellbeing tools section at the top of the page. 3. You can see your current app usage statistics front-and-center on the screen. To see a weekly report of your screen time in apps, tap the graph icon at the top right of the screen.

How do I track mobile app usage?

Digital Wellbeing - Android Similar to Screen Time, the tool picks provides insight on how much time you spend on different apps, like Spotify, YouTube and Whatsapp. Obviously, this tracker's effectiveness depends on how open you are to actually policing your phone habits once they've been identified.


1 Answers

First thing , that's required to be known here is what are the applications that are running in the foreground :

You can detect currently foreground/background application with ActivityManager.getRunningAppProcesses() call.

So, it will look something like ::

  class findForeGroundProcesses extends AsyncTask<Context, Void, Boolean> {

      @Override
      protected Boolean doInBackground(Context... params) {
        final Context context = params[0].getApplicationContext();
        return isAppOnForeground(context);
      }

      private boolean isAppOnForeground(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
          return false;
        }
        final String packageName = context.getPackageName();
        for (RunningAppProcessInfo appProcess : appProcesses) {
          if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
            return true;
          }
        }
        return false;
      }
    }

    // Now  you call this like:
    boolean foreground = new findForeGroundProcesses().execute(context).get();

You can probably check this out as well : Determining foreground/background processes.

To measure the time taken by a process to run its due course , you can use this method :

getElapsedCpuTime()  

Refer this article .

like image 64
The Dark Knight Avatar answered Sep 30 '22 10:09

The Dark Knight