有没有办法查找应用程序自上次使用以来关闭的时间?我正在编写一款应用程序,根据用户多久没有使用该应用程序来操作数据。例如:如果用户每小时不访问该应用程序,则称为“分数”的数据会减少10个。因此,如果用户已经两个小时没有打开应用程序,“分数”将减少20。
所以我想知道是否有可能,并且如果是,我应该如何实现它呢?
谢谢!
所以我想知道是否有可能,并且如果是,我应该如何实现它呢?
谢谢!
在你的onPause
中将系统时间存储在你的SharedPreferences
中:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putLong("last_used",System.currentTimeMillis()).commit();
在您的onResume
中读取值"last_used"并进行计算:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
long lastTime = prefs.getLong("last_used");
long currentTime = System.currentTimeMillis();
//Now do the calculations as you like