0

I am working on an application for which i want to track how much time my app is up (foreground + background).
I found some threads, but i can't find any solution from them

SO can anyone please let me know if this is possible and if yes how to achieve that?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
madhu makwana
  • 49
  • 1
  • 7

1 Answers1

0

You can get time in onCreate method of your custom Application class, save it (or keep as static) and read the difference in other place

Tomasz Czura
  • 2,414
  • 1
  • 14
  • 18
  • Yes but onCreate will only call when app will be in foreground,what about if app is in background?i want total uptime(background+foreground) of app,till it is killed.please please help me if you know this. – madhu makwana Jul 26 '18 at 09:44
  • onCreate Application's class is called in background too – Edu Romero Jul 26 '18 at 09:46
  • Ok i will try this.but where to read difference as yiu said get difference in other place? – madhu makwana Jul 26 '18 at 12:19
  • @madhumakwana "other place" means upto when you want to calculate uptime. calculate the difference between current time and the time stored in Application class – Vihaari Varma Jul 26 '18 at 12:23
  • I can get this way but i also need time when app is killed but its background service is running. – madhu makwana Jul 26 '18 at 12:29
  • @VihaariVarma -Can you please read my last comment?i want to achieve that.So if you have any idea regarding this,so please help me. – madhu makwana Jul 26 '18 at 13:45
  • When background service is running, the app is not killed – Tomasz Czura Jul 26 '18 at 14:57
  • @madhumakwana What you need to do is to save the time when onCreate() is called in your custom Application class and calculate the time difference when onDestroy() was called. The Application class will be instantiated every time your app's components get started ie. Activity or a Service. [See this](https://github.com/codepath/android_guides/wiki/Understanding-the-Android-Application-Class) – Vihaari Varma Jul 27 '18 at 04:22