简体   繁体   中英

How to track app usage time programmatically in android?

I want to know how much time user is using my app . So whenever he opens my app I want to know time and whenever he close app i want to know time. When user will close app i want to send usage time to server. How can i track usage time in android ?

The way I mentioned below is a very efficient way to get the total spent a time of an app by a user in android. Actually I also use this approach to reward my users for their time spent. 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;
   /**startingTime is the UNIX timestamp (though I increased readability by converting into millisecond to second) your app is being foregrounded to the user and stopTime is when it is being backgrounded (Paused)*/
    @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. Here you can post the data of total spending time to the server.
        }
    }

    @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

I would put data loges onResume and onPause methods. and on every onPause Append usage time to SharedPrefrences or some database and on requirements on create or when internet is turned on i would send usage statistics.

In this manner you can track every activities usage.

Not sure if some of the box methods exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM