0

how can I get my application uptime?

I found this: https://stackoverflow.com/a/6431668/3973591

But i have this error:

D/Error: ERR: file=BaseDexClassLoader.java
D/Error: ERR: class=dalvik.system.BaseDexClassLoader
D/Error: ERR: method=findClass line=56
D/Error: ERR: stack=java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/management/ManagementFactory;

Can you help me?

Or do you have another solution?

Roham Rafii
  • 2,929
  • 7
  • 35
  • 49

2 Answers2

4

how i can get my application uptime ?

"Uptime" does not really have a lot of meaning in Android. But, you are welcome to:

  • Create a subclass of Application
  • Override onCreate() and make note of the current time (e.g., SystemClock.elapsedRealtime())
  • Register that custom Application subclass in the manifest, via android:name on the <application> element

Then, your process' uptime is the current time minus the value saved in onCreate().

i found this :https://stackoverflow.com/a/6431668/3973591

That code principally is for Java servers, and that class does not exist on Android.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you for your quick response. Already i have Application. Also i think about this. But only we have this solution ? Android does not give you uptime ? –  Dec 08 '16 at 14:03
  • 1
    @Hasan: "Android does not give you uptime ?" -- not that I can think of. Again, it is not a useful metric on Android IMHO. Processes are supposed to come and go as needed. – CommonsWare Dec 08 '16 at 14:04
  • 3
    There can be a need to track and test uptime, for digital signage or kiosk applications where a single Application is long running – Ian Butler Nov 16 '18 at 04:48
0

As of API 24, the following were added to Android:

Either of the following should work to provide an application uptime:

val appUptimeMs =  SystemClock.elapsedRealtime() - Process.getStartElapsedRealtime()
val appUptimeMs = SystemClock.uptimeMillis() - Process.getStartUptimeMillis()

If running on a pre-24 device, you will need a store a start time at application startup, as mentioned by CommonsWare.

Anm
  • 3,291
  • 2
  • 29
  • 40