Job Scheduler problem

SamoSSamoS Member ✭✭

Hi all

I would like to implement JobScheduler which is a great substitution for a classical background service. I tried to test the simple scenario where the Job Scheduler is started when the application goes to background or is closed. The Job Scheduler is canceled at the moment when the application starts opening or resuming. For this purpose, I wrote the following code in App class

protected override void OnStart()
{
var message = new StopLongRunningTaskMessage();
MessagingCenter.Send(message, "StopLongRunningTaskMessage");
}

    protected override void OnSleep()
    {
        var message = new StartLongRunningTaskMessage();
        MessagingCenter.Send(message, "StartLongRunningTaskMessage");
    }

    protected override void OnResume()
    {
        var message = new StopLongRunningTaskMessage();
        MessagingCenter.Send(message, "StopLongRunningTaskMessage");
    }

The method OnCreate in MainActivity class has the following code:

MessagingCenter.Subscribe(this, "StartLongRunningTaskMessage", message =>
{

                if (!IsJobServiceOn())
                {
                    _jobScheduler = (JobScheduler)GetSystemService(JobSchedulerService);
                    var extra = new PersistableBundle();
                    extra.PutInt("LoopCount", 20);
                    var javaClass = Java.Lang.Class.FromType(typeof(MyFirstservice));
                    var compName = new ComponentName(this, javaClass);
                    var jobInfo = new JobInfo.Builder(1, compName)
                                              .SetPersisted(true)
                                              .SetExtras(extra)
                                              .SetPeriodic(900000)
                                              .Build();

                    var result = _jobScheduler.Schedule(jobInfo);                     
                }         

        });

        MessagingCenter.Subscribe<StopLongRunningTaskMessage>(this, "StopLongRunningTaskMessage", message => {
            if (IsJobServiceOn())
                _jobScheduler.Cancel(1);
        });
    }


    private bool IsJobServiceOn()
    {
        JobScheduler scheduler = (JobScheduler)GetSystemService(JobSchedulerService);

        if (scheduler.GetPendingJob(1) != null)
            return true;
        else
            return false;
    }

The JobScheduler works fine as long as the application is not swiped out or closed entirely. I really do not know why the application crashes when it is restarted after it was swiped out from the recent app list.

Thanks for help in advance.
Samo

Answers

  • SamoSSamoS Member ✭✭

    @SamoS said:
    Hi all

    I would like to implement JobScheduler which is a great substitution for a classical background service. I tried to test the simple scenario where the Job Scheduler is started when the application goes to background or is closed. The Job Scheduler is canceled at the moment when the application starts opening or resuming. For this purpose, I wrote the following code in App class

    protected override void OnStart()
    {
    var message = new StopLongRunningTaskMessage();
    MessagingCenter.Send(message, "StopLongRunningTaskMessage");
    }

        protected override void OnSleep()
        {
            var message = new StartLongRunningTaskMessage();
            MessagingCenter.Send(message, "StartLongRunningTaskMessage");
        }
    
        protected override void OnResume()
        {
            var message = new StopLongRunningTaskMessage();
            MessagingCenter.Send(message, "StopLongRunningTaskMessage");
        }
    

    The method OnCreate in MainActivity class has the following code:

    MessagingCenter.Subscribe(this, "StartLongRunningTaskMessage", message =>
    {

    if (!IsJobServiceOn())
    {
    _jobScheduler = (JobScheduler)GetSystemService(JobSchedulerService);
    var extra = new PersistableBundle();
    extra.PutInt("LoopCount", 20);
    var javaClass = Java.Lang.Class.FromType(typeof(MyFirstservice));
    var compName = new ComponentName(this, javaClass);
    var jobInfo = new JobInfo.Builder(1, compName)
    .SetPersisted(true)
    .SetExtras(extra)
    .SetPeriodic(900000)
    .Build();

                        var result = _jobScheduler.Schedule(jobInfo);                     
                    }         
    
            });
    
            MessagingCenter.Subscribe<StopLongRunningTaskMessage>(this, "StopLongRunningTaskMessage", message => {
                if (IsJobServiceOn())
                    _jobScheduler.Cancel(1);
            });
        }
    
    
        private bool IsJobServiceOn()
        {
            JobScheduler scheduler = (JobScheduler)GetSystemService(JobSchedulerService);
    
            if (scheduler.GetPendingJob(1) != null)
                return true;
            else
                return false;
        }
    

    The JobScheduler works fine as long as the application is not swiped out or closed entirely. I really do not know why the application crashed when it is re-opened after swiping out from the recent app list.

    Thanks for help in advance.
    Samo

Sign In or Register to comment.