简体   繁体   中英

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, 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM