Android: 设置定时器允许开启activity

Android: set timer to allow opening activity

我在我的应用程序中有一个旋转赢取 activity,用户可以用它来获得金币。 我想在用户使用微调器后,他必须等待 8 小时才能再次打开旋转器 activity。 怎么做?

一种方式是:

  • 从他旋转的那一刻算起那8个小时->

    long duration = System.currentTimeMillis() + your8hours;
  • 保存此数据(共享偏好)->
    SharedPreferences sharedPreferences = new SharedPreferences();
    sharedPreferences = getSharedPreferences("MYDURATIONSAVED", Context.MODE_PRIVATE)
    Editor editor = sharedPreferences.edit();
    editor.putInt("durationSaved", duration);
    editor.apply();
  • 在新会话中,您检索数据 ->
    long alreadyExistingDuration = sharedPreferences.getLong("durationSaved", -1);
  • 并简单地比较它以检查是否允许用户再次旋转 ->
    long check = System.currentTimeMillis();

    if(check >=  alreadyExistingDuration){
    allow...
    }

如果你能放一个你的代码示例我会用它 例如,如果您使用 sqlite、firebase 或文件来存储您的数据,则可能会有不同的答案 但是您需要存储的数据是原始数据,因此 sharedpreference 应该足够了