简体   繁体   中英

How to know for how much long the application is running in android?

I am doing an application in android which requires to know for how long the application is running. Do anyone know how to retrieve such information??. Is there any way where android provides the information about the running applications ,from how much time they are running??

I'm not aware of any method which would handle that but you can simply implement it yourself. Just capture the current time in seconds and in the onPause() method do the same. Then just subtract the first saved time value and the last saved value and you should know for how many seconds the application was running.

There might be another more elegant solution I don't know of tho.

You could user SystemClock.uptimeMillis() or SystemClock.elapsedRealtime() in your main activity once it is first launched. Then each time you need to see how long the app is running call the method again and subtract from the original value.

There is some data collected for all applications to show battery usage by application, but I've not done the research to find out how to access it programmatically (if even possible) and am not sure when it is reset (perhaps on every charger/usb disconnect).

For one application the previous suggestions about instrumenting create and pause/resume methods sounds best.

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