0

How to Get the Time spent on an specific application using packagename in Android Programmatically?

After doing google I found this stack overflow question and this but I didn't get any proper solution.

Does any one know how to I get that time for all android version?

I also tried this but I don't want to do background service. When user back to my app I just want to know that does user used that app for given time or not.

Community
  • 1
  • 1
a.dev
  • 207
  • 2
  • 11

2 Answers2

1

You have to do two things for achieve that.

1. Start timer when user left your activity and goes to another app.

2. call background service using below code

private void check()     
{
    ActivityManager actvityManager = (ActivityManager)
            this.getSystemService( ACTIVITY_SERVICE );
    List<ActivityManager.RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
    for(int i = 0; i < procInfos.size(); i++)
    {
        if(procInfos.get(i).processName.equals("specific pacakgename here")) {
           
            //check time here

        }
    }
}
Community
  • 1
  • 1
Bhavin Patel
  • 872
  • 13
  • 30
1

The way I mentioned below is a very efficient way to get the total spent time of an app by a user in android. First of all you need to a custom ActivityLifecycleCallbacks which is responsible to trigger out when the application began to start and when stop. You need to just register the ActivityLifecycleCallbacks to your application instance. This will also care about if any user pauses the app so you don't need to think about it.The custom ActivityLifecycleCallbacks class is -

public class ApplicationTimeSpentTracker implements Application.ActivityLifecycleCallbacks {
    private int activityReferences = 0;
    private boolean isActivityChangingConfigurations = false;
    private long startingTime;
    private long stopTime;

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
        if (++activityReferences == 1 && !isActivityChangingConfigurations) {

            // App enters foreground
            startingTime=System.currentTimeMillis()/1000; //This is the starting time when the app began to start
        }
    }

    @Override
    public void onActivityResumed(Activity activity) {
    }

    @Override
    public void onActivityPaused(Activity activity) {
    }

    @Override
    public void onActivityStopped(Activity activity) {
        isActivityChangingConfigurations = activity.isChangingConfigurations();
        if (--activityReferences == 0 && !isActivityChangingConfigurations) {

            // App enters background
            stopTime = System.currentTimeMillis() / 1000;//This is the ending time when the app is stopped 

            long totalSpentTime= stopTime-startTime; //This is the total spent time of the app in foreground
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }
}

Finally you need to register the callback to your application instance in MainActivity like this -

    activity.getApplication().registerActivityLifecycleCallbacks(new ApplicationTimeSpentTracker());

Here is my post if you need details

Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42