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 ?
-
Which part are you having trouble with? Getting the time? Where to put the calls that retrieve the time/upload to server? How to upload the time to the server? Make your question a little more specific. – Breavyn Oct 01 '15 at 06:50
-
use `Application` class `onCreate()` and `onDestroy()` – Rustam Oct 01 '15 at 06:55
-
i want to track session time . whenever user open and closes app I want to track time . i just need time. – Mahesh Oct 01 '15 at 06:59
-
http://stackoverflow.com/questions/5369682/get-current-time-and-date-on-android – Breavyn Oct 01 '15 at 07:03
-
@Rustam there is no onDestroy() in Application . onTerminate() is there but i don't know will it solve my problem(tracking app usage time). – Mahesh Oct 01 '15 at 12:41
-
Check this http://developer.android.com/reference/android/app/usage/package-summary.html This is for Lollipop and above. May be this is helpful for you – Zeeshan Dec 16 '15 at 22:23
2 Answers
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

- 6,084
- 3
- 42
- 42
-
3
-
Start time is the UNIX timestamp (though I increased readability by converting into millisecond to second) your app is being foregrounded to the user and Stop time is when it is being backgrounded (Paused). – Gk Mohammad Emon Jul 13 '20 at 07:08
-
That's means When apps go to the background that time start time gets value? – Masum Jul 13 '20 at 07:40
-
2actually you can consider it as a session. That means when the user first open the app ,spend some time then go to the background. When he goes to the background than "totalSpentTime" will be got which is the time of the first session. We can store the totalSpentTime somewhere in local database (like sharedPreference). Then when the user again comes to foreground then the 2nd session start. If you sums the all "totalSpentTimes" then we will get the total spend time spent by an suer in your app. – Gk Mohammad Emon Jul 13 '20 at 08:50
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.

- 1,754
- 3
- 21
- 39
-
but this way we can only track usage per activity . Is there any way to track usage time for app? – Mahesh Oct 01 '15 at 12:38
-
Rustam answered actually my other idea. But i doubt it's accurate sinc you can have your app in background. You have to try to know if it works. – Alpha Oct 01 '15 at 20:48