William Papsco
William Papsco

Reputation: 255

Scheduling Actions or Alarms in Android

So, I'll start by saying I have already done a lot of research on this, but I'm still stuck.

So here's the goal: Preform a database action (insert a reminder message into local sqlitedb) and send a notification at pre-set times given by some user settings.

And here's the problem: It doesn't work when the app is closed.

I have tried using AlarmManager with a broadcast pending intent and with a service pending intent. Neither seemed to work in the problem scenario. After looking into it some more, I found that on newer versions of Android, they are paring back some of the functionality of AlarmManager in favor of JobScheduler, a new API that 1) seems to be overkill for what I'm doing and 2) won't work on older devices. Sending alarms while the app is closed seems to be one of those "pared-back" features. What should I do? What do I use? What does everyone else usually do? Have I misunderstood something? Please help; I can't find anything on Google!

Upvotes: 1

Views: 50

Answers (1)

Zachary Sweigart
Zachary Sweigart

Reputation: 1111

There are a few methods of running "jobs" pre-lollipop.

You can use the JobSchedulerCompat library, but this library has a huge difference compared to the default Lollipop API:

On Lollipop jobs are batched and scheduled for all apps at once. The library however does not have access to other apps data and therefore can't combine jobs from two different apps using the library.

Another option is to use JobScheduler for lollipop and greater devices and then use AlarmManager for pre-lollipop devices by checking if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){}

Finally, you can use the Sync-Adapter API. The Sync adapter encapsulates the code for the tasks that transfer data between the device and a server. Based on the scheduling and triggers you provide in your app, the sync adapter framework runs the code in the sync adapter component. To add a sync adapter component to your app, you need to add the following pieces:

  • Sync adapter class.
  • Bound Service.
  • Sync adapter XML metadata file.
  • Declarations in the app manifest.

Upvotes: 2

Related Questions