0

I observed one problem regarding periodic JobScheduler. It works as expected as long as the application is not force closed. In this case, the JobScheduler is triggered three times regardless of the fact that the method Schedule() is called only once. In my case, the JobScheduler is started when OnStop() method is called and stopped when the OnStart() method is called. It means that JobScheduler works as long as the application is in the background.

The simplified code inside the JobScheduler:

public override bool OnStartJob(JobParameters @params)
{

    CancellationToken token = tokenSource.Token;

    bleTask = Task.Run(async() =>
    {
        await ScanForDevice(@params, token);
    },token);

    return true; 

}

public override bool OnStopJob(JobParameters @params)
{

    if(bleTask != null)
    {

        if (bleTask.Status == TaskStatus.WaitingForActivation)
            tokenSource.Cancel();

    }
    return false;
}

public async Task ScanForDevice(JobParameters jobParams, CancellationToken  token)
{


   for (int i = 0; i < 120; i++)
   {
        if (token.IsCancellationRequested)
        {     
           return;
        }

        await Task.Delay(500);  // piece of code is simulated with some delay
   }
   JobFinished(jobParams, false);

}

I really do not understand why the JobScheduler is executed three times in the case of the application is force closed. It is very interesting if OnStartJob() return value is false then the mentioned problem is not possible to reproduce.

1 Answers1

0

If your OnStopJob() method returns true it means to Android: reschedule the job. For reference see the documentation.

true to indicate to the JobManager whether you'd like to reschedule this job based on the retry criteria provided at job creation-time; or false to end the job entirely. Regardless of the value returned, your job must stop executing.

From what I can tell you have already solved the problem by returning false which is the correct way of doing it. If the application gets force-terminated, the job is not supposed to be running.

Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • Thanks for the reply. I agree that the job is canceled if the return value of `OnStartJob()` is false. But the problem is that we do not know in which part of the code the method `ScanForDevice()` is canceled. This is important for me because the method in my real application starts and stops Bluetooth scanning and I need to know in what state the Bluetooth is (scanning or not scanning). Is it possible to solve this issue? – Samo Simoncic Feb 19 '19 at 11:10
  • I do not understand why you need to know whether ScanForDevice() is scanning or not scanning. If your app is being force terminated, Android will automatically stop your scan if it is already running. If you just need to record this for future reference, then I suppose you could write to persistent storage each time you start or stop scanning. That way when your app eventually restarts, you can see what the scan state was when the app was last running. – davidgyoung Feb 20 '19 at 02:28
  • Thanks for the reply. If I am honest I did not know that the Android automatically stop scanning when the app is being force terminated. – Samo Simoncic Feb 21 '19 at 18:50