Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Calculate duration that user hasn't been using the applcation

Tags:

android

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!

like image 970
chique Avatar asked Jan 21 '26 12:01

chique


1 Answers

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
like image 180
Pooya Avatar answered Jan 23 '26 20:01

Pooya