Is there anyway to find out how long the app has been closed since the last use? I am writing an application that manipulates data depends on how long user hasn't been using the application. For example: For every hour that the user doesn't visit the app, data called "score" reduces by 10. So if user hasn't opened the app for 2 hours, "score" reduces by 20.
So I am just wondering if it is possible, and if yes, how can I achieve it?
Thanks!
in your onPause store system time in your SharedPreferences:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putLong("last_used",System.currentTimeMillis()).commit();
and in your onResume read the value "last_used" and do the calculation:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
long lastTime = prefs.getLong("last_used");
long currentTime = System.currentTimeMillis();
//Now do the calculations as you like
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With