How can I run background work like alarm on latest android phones

问题: I have some task like medicine reminder. Everyday reminder schedule is stored in the SQLite database. Reminders are every day different. For example 21.02.19 - 08:00,...

问题:

I have some task like medicine reminder. Everyday reminder schedule is stored in the SQLite database. Reminders are every day different.

For example

21.02.19 - 08:00, 12:00, 19:00

22.02.19 - 08:30, 12:30, 19:30

...

Minimum android 4.4 (Api 19) Target android 9.0 (Api 28)

I read about WorkManager, JobScheduler, JobIntentService, FirebaseJobDispathcer, Alarm manager. There is a lot of API s to do that.

Firstly I tried to check time with interval every minute using alarm manager. But it is not working properly in latest android. There have background restrictions, Doze Mode and standby. Then I tried JobScheduler but it does not work devices under 21 API. And FirebaseJobDispatcher required google play service, it is not a good idea. Finally, I find WorkManager. But in workmanager and jobscheduler have minimum interval like 15 minutes.

How can I make background service to remind users in specific exact time every day? I am not using the internet, GPS, location. Only get time from SQLite and remind in exact time.


回答1:

In a few months ago, I developed medicine reminder app too.

I use 'run the daily job in 0:00AM using android-job' methods to achieve this.

In DailyJob, we have this process.

  1. Grab some time information in DB
  2. Convert (desired time - now time) into milliseconds. if now time is 00:15AM and desire time is 07:00AM, it will be 24300000ms.
  3. Run exact job with converted milliseconds in Step 2.

In Exact job, we display a notification to the user.

I can't attach code because of privacy, but you can achieve the same things.

  • 发表于 2019-02-22 14:32
  • 阅读 ( 242 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除