Forum Xamarin Xamarin.Android

Android Job Scheduler - calling a job on a recurring schedule until cancelled?

RhonageRhonage NZMember ✭✭

Hi all,

With the release of Oreo/Android 8.0, I'm wanting to replace my GPS foreground service with a scheduled job.

I've looked at the Xamarin Example for Job Scheduler, and honestly I find it quite confusing.

Here is all I want to do:

  1. User taps a button
  2. A job is created and scheduled.
  3. The job is called instantly, and every 30 seconds afterwards until the user taps the button again

The job will be the replacement for my GPS service (which implementes ILocationListener).

Could anyone help clear up what I need to do to achieve this?

I have tried .SetPeriodic(30000), which doesn't seem to work on my test device running Oreo.

The example uses .SetMinimumLatency() and .SetOverrideDeadline() to set some sort of schedule range. Can I set both of these to 30000 (30 seconds) to achieve what I need?

The example uses handlers and passes parameters, etc. Is this required for a Job Scheduler? There's a lot going on for a basic example project, and I'm just getting confused about the requirements.

Thanks in advance, hoping someone can help me clear this up a bit.

Cheers.

Posts

  • kevinskrei2kevinskrei2 Member

    So, Job Scheduler is meant for performing tasks when the OS wants to. This is used to conserve battery life and run multiple jobs from multiple apps at the same time (only have to wake up the CPU once).

    If you need to run a task exactly every 30 seconds you don't want to use JobScheduler. Use an alarm manager or a simple handler with a postDelayed. I believe there is somewhere in the documentation where it states the minimum interval a periodic job can run is 15 minutes.

  • RhonageRhonage NZMember ✭✭
    edited March 2018

    Thanks for your reply @kevinskrei2.

    Yes you are correct, the minimum job scheduled time is 900,000ms for Oreo (15 minutes).

    In production, I will be calling for a GPS location closer to the 15 minute mark, but I'd like the flexibility to go as low as 5 mins when needed. I can do this 100% fine with a Foreground Service, but due to the changes in Oreo it always gets moved into the background after a time.

    Any idea what my options could be to keep it running? My thoughts so far are:

    • Keep it as it is, but schedule a job to run every 15 minutes to ensure that the Foreground service is still running.
    • Look into PendingIntents, apparently they might be an option
    • Use an Alarm Manager as you suggest (coupled with a Notification)

    The only thing is I want the user to know when the GPS services are enabled, so I'd like to display a Notification to them (which Foreground service provides).

    Cheers

  • GmotagiGmotagi GBMember ✭✭
    edited March 2018

    Rene has a lightning lecture that explains the scheduler well. You can find it in the menu or on YouTube here.
    https://youtube.com/watch?v=aSjBBPYjelE

Sign In or Register to comment.