【问题标题】:Adding an item to Room Database at specific time every day每天在特定时间将项目添加到房间数据库
【发布时间】:2020-07-09 12:06:36
【问题描述】:

我需要代码才能在每天上午 12 点将项目添加到 Room Database。我尝试了很多方法,例如WorkManager和AlarmManager,但都没有达到这个目标

【问题讨论】:

  • 当您的应用程序打开时,只需制作某种计时器。当用户关闭应用程序时,将时间保存在共享首选项中。当用户打开应用时,使用关闭时间作为参考添加缺失的项目。
  • 你能给我更多的细节吗
  • 你使用 Room 吗?
  • 是的,我用了房间!

标签: java android android-studio scheduled-tasks android-room


【解决方案1】:

这是我在评论中提到的实现:

public class MainActivity extends AppCompatActivity {
    private static final String PREF_PAUSE_TIME_KEY = "exit_time";

    private static final Long MILLIS_IN_DAY = 86400000L;

    private static final int TRIGGER_HOUR = 12;
    private static final int TRIGGER_MIN = 0;
    private static final int TRIGGER_SEC = 0;

    private final Handler handler = new Handler();
    private SharedPreferences prefs;

    private final Calendar calendar = Calendar.getInstance();

    private final Runnable addItemRunnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(addItemRunnable, MILLIS_IN_DAY);
            addItem();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //...

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Add missing events since onPause.
        long resumeTime = System.currentTimeMillis();
        long pauseTime = prefs.getLong(PREF_PAUSE_TIME_KEY, resumeTime);

        // Set calendar to trigger time on the day the app was paused.
        calendar.setTimeInMillis(pauseTime);
        calendar.set(Calendar.HOUR_OF_DAY, TRIGGER_HOUR);
        calendar.set(Calendar.MINUTE, TRIGGER_MIN);
        calendar.set(Calendar.SECOND, TRIGGER_SEC);

        long time;
        while (true) {
            // If calendar time is during the time that app was on pause, add item.
            time = calendar.getTimeInMillis();
            if (time > resumeTime) {
                // Past current time, all items were added.
                break;
            } else if (time >= pauseTime) {
                // This time happened when app was on pause, add item.
                addItem();
            }

            // Set calendar time to same hour on next day.
            calendar.add(Calendar.DATE, 1);
        }

        // Set handler to add item on trigger time.
        handler.postDelayed(addItemRunnable, time - resumeTime);
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Save pause time so items can be added on resume.
        prefs.edit().putLong(PREF_PAUSE_TIME_KEY, System.currentTimeMillis()).apply();

        // Cancel handler callback to add item.
        handler.removeCallbacks(addItemRunnable);
    }

    private void addItem() {
        // Add item to database and RecyclerView.
    }
}
  • 调用onPause 时,当前时间保存在首选项中。
  • 当调用onResume 时,程序会添加在未打开应用时应添加的所有项目。
  • 打开应用后,程序会使用Handler 在特定时间发布任务。

您可以将代码移动到您想要的任何地方,最有可能在您的存储库或视图模型中(如果有的话)。对于计时器,您可以将 RxJava 用于应用内计时器或其他任何东西。

如果用户将时间更改为提前几天,添加的项目将不会被删除。如果用户随后将其设置为正常,则某些天的项目将被复制。您可以通过仅保存onPause 中的时间(如果它大于上次保存的时间)来防止这种情况发生。

【讨论】:

  • 什么不起作用?您可以尝试添加日志语句或断点以查看调用的内容和未调用的内容。一个月前我测试它时它起作用了。
  • 但是当onPause所有项目都消失@Nicolas时出现问题
  • 从哪里来的?数据库,还是你的列表?问题一定出在你的代码上,我给你的代码里连添加项目的逻辑都没有。
  • 请在我的列表中帮助我
  • 您的房间数据库中有这些项目吗?如果不是,它们肯定会在应用退出时被删除。如果是,那么只需查询您的数据库以显示项目。如果没有看到您的代码,我真的无法说出为什么它们会消失。发布一个新问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-14
相关资源
最近更新 更多