Get current app running time


859 просмотра

4 ответа

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 ?

Автор: jay Источник Размещён: 08.11.2019 11:03

Ответы (4)

1 плюс


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.

Автор: Elltz Размещён: 20.08.2016 09:01

2 плюса

you can use this code to know how time is running....

declare variables....

long startTime, endTime ,duration ;

In your onStart() Methods do this....

protected void onStart() {
     startTime = System.currentTimeMillis();

In your onStop() Methods do this....

protected void 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...

Автор: yosef abu rabeaa Размещён: 20.08.2016 12:22

1 плюс

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.


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):

How to use UsageStatsManager?

Автор: Vyacheslav Gerasimov Размещён: 20.08.2016 08:47

0 плюса

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.

Автор: Alessandro Roaro Размещён: 20.08.2016 08:18
Вопросы из категории :