title: Android Job Scheduler only running jobs when app is open on Android M (6)
body: <p>I am trying to add an Android Job Scheduler to run jobs in the background on my Xamarin Forms app but it only seems to run when I have the app open.</p>
  
  <p>The job is being scheduled correctly but just not firing with the app closed. What is supposed to happen when the job fires in an external API needs to be called to get some data and then a local SQLite database needs to be updated with that data.</p>
  
  <p>I am testing this on a OnePlus 2 with Android 6 (M).</p>
  
  <p>Below is my job service implementation.</p>
  
  <pre><code>[Service(Name = "com.sample.JobSchedulerService", Permission = "android.permission.BIND_JOB_SERVICE")]
  public class JobSchedulerService : JobService
  {
      private readonly Service _service;
  
      public JobSchedulerService()
      {
          _service = new Service();
      }
      public override bool OnStartJob(JobParameters jobParams)
      {
          Task.Run(() =&gt;
          {
              var updated = _service.UpdateDataAsync(54, -2);
              JobFinished(jobParams, !updated.Result);
          });
  
          return true;
      }
  
      public override bool OnStopJob(JobParameters jobParams)
      {
          return true;
      }
  }
  </code></pre>
  
  <p>Below is the code which schedules my job.</p>
  
  <pre><code>[assembly: Xamarin.Forms.Dependency(typeof(DownloadJob))]
  namespace IslamReminders.Droid.Services
  {
  public class DownloadJob : IDownloadJob
  {
      public void ScheduleJob(int jobId, long notificationRepeatMilliseconds)
      {
          var jobBuilder = JobScheduleHelpers.CreateJobBuilderUsingJobId&lt;JobSchedulerService&gt;(Application.Context, jobId)
                               .SetPeriodic(notificationRepeatMilliseconds)
                               .SetPersisted(true)
                               .Build();
  
          var jobScheduler = (JobScheduler)Application.Context.GetSystemService(Context.JobSchedulerService);
          var scheduleResult = jobScheduler.Schedule(jobBuilder);
      }
  }
  }
  </code></pre>
  
  <p>Below is the code to call the DownloadJob to schedule the job from App.cs OnStart.</p>
  
  <pre><code>protected override void OnStart()
      {
          // Handle when your app starts
          var jobs = _dataAccess.GetJobs();
  
          if (jobs.Any(x =&gt; !x.IsActive))
          {
              var inactiveJobs = jobs.Where(x =&gt; x.IsActive == false);
  
              foreach (var job in inactiveJobs)
              {
                  var notificationTimes = _dataAccess.GetNotificationTimesForJob(job.NotificationId);
  
                  foreach (var notificationTime in notificationTimes)
                      DependencyService.Get&lt;IDownloadJob&gt;().ScheduleJob(job.Id, (long)notificationTime.NotificationRepeat.TotalMilliseconds);
  
                  job.IsActive = true;
                  _dataAccess.UpdateJob(job);
              }
          }
      }
  </code></pre>
  
  <p>I think I have everything setup correctly but just not working when the app is closed. Any help with this will be greatly appreciated.</p>