每次数据库获取新数据时自动创建警报

时间:2016-04-21 15:10:11

标签: android android-alarms

每次在数据库中插入新数据时我都需要创建一个警报(数据库具有将要创建的每个警报的日期和时间值),而不与用户进行任何交互,应用程序必须自动完成。

  • 如何知道何时插入新数据?使用计时器检查每x分钟?
  • 如果发生这种情况,如何自动创建警报?

1 个答案:

答案 0 :(得分:0)

通常对于类似这样的内容,您可能希望使用更像Firebase的内容来更新您的数据。

但要做一个计时器,首先要扩展TimerTask:

public class Progress extends TimerTask {
        @Override
        public void run(){
            //this item runs on a separate thread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    seconds += 0.1;
                    //this item runs on the main thread

                }

            });
        }
    }

然后运行此任务:

Timer timer;
Progress progress;
timer.schedule(progress, 100, 100);

这将每隔0.1秒运行一次计时器。