我是android的新手.我正在创建一个应用程序,我必须在单击后禁用该按钮,并在5分钟后再次启用它.即使应用程序关闭后,倒计时器仍应继续.任何帮助表示赞赏.提前致谢!
您可以通过以下代码将时间存储在共享首选项中.
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 1);
SharedPreferences.Editor editor = settings.edit();
editor.putString("click_time", your_time);
Run Code Online (Sandbox Code Playgroud)
无论何时需要比较,只需将当前时间与优先存储的时间进行比较.检查时间差是否小于5分钟.
从偏好中获得时间:
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 1);
String storedTime=settings.getString("clic_time", "");
Run Code Online (Sandbox Code Playgroud)
分钟差异:
Date current, Date previous;
long diff = current.getTime() - previous.getTime();
long minutes = diff / (60 * 1000);
Run Code Online (Sandbox Code Playgroud)
要显示计时器,当应用程序来自后台时,您可以从首选项中获取时间并使用2次之间的差异启动计时器.
| 归档时间: |
|
| 查看次数: |
2663 次 |
| 最近记录: |