I want to know the detail - how long has my app been active in a day ? I cannot is explicit storing of start time and end time as the app is deployed on plug pcs (which do not have a battery, so onDestroy would not be called). Is there a way to get app running time information by just using system calls ?
4 Answers
you can use this code to know how time is running....
declare variables....
long startTime, endTime ,duration ;
In your onStart() Methods do this....
@Override
protected void onStart() {
super.onStart();
startTime = System.currentTimeMillis();
}
In your onStop() Methods do this....
@Override
protected void onStop() {
super.onStop();
endTime = System.currentTimeMillis();
}
and calculate the result.....
duration = (endTime - startTime);
Then store the duration every time and add it to others
the time in milliseconds...

- 139
- 1
- 4
-
Can rely on onStop, because - app is deployed on plug pcs (which do not have a battery) so cant ensure onStop will be called when device is powered off. – jay Aug 21 '16 at 07:52
-
-
-
There is a UsageStatsManager which is available from API level 21. It can be used to check if your app is active or get application usage stats.
NOTE: This API requires the permission android.permission.PACKAGE_USAGE_STATS, which is a system-level permission and will not be granted to third-party apps. However, declaring the permission implies intention to use the API and the user of the device can grant permission through the Settings application.
queryUsageStats
List<UsageStats> queryUsageStats (int intervalType, long beginTime, long endTime)
Gets application usage stats for the given time range, aggregated by the specified interval. The returned list will contain a UsageStats object for each package that has data for an interval that is a subset of the time range given.
Here is sample app (found in other so question):

- 1
- 1

- 1,334
- 9
- 10
or you could do this yourself? with creating an extra background thread that runs when the Application
singleton class starts by calling the onCreate()
in your while loop you could add wait
or sleep
to your Thread for the interval you want, or you could just use TimerTask
, store your values to your variables and persist them in sharedPreference or so, just for incase.
Then call it whenever you want.

- 10,730
- 4
- 31
- 59
-
-
-
1Although your solution does work for sure, I have used a local sqlite db to store session history without needing another thread. Thanks though. – jay Aug 25 '16 at 06:31
-
you are welcome sir, but if you please you could post your answer and accept it might help someone. yours seems very legit too.@jay – Elltz Aug 25 '16 at 10:24
No, there is no way to do that.
Even so, if onDestroy
is not going to be called (which is not reliable in any case, because it's not called every time the activity is destroyed), you could store a "start value" in the onResume
of your main Activity
and calculate the usage time when onPause
is called.
Then you add this value to the total usage of the day.

- 4,665
- 6
- 29
- 48
-
As I've mentioned - the device which uses my app are plug pcs - don't have battery, they run with continuous power supply. When power off, onPause, onDestroy will not be called. – jay Aug 20 '16 at 08:36