Reputation: 2951
I have a Xamarin app which runs a repeating ScheduledJob.
If the app is force closed, when the scheduled job next runs, the app crashes.
Why is the scheduled job still being run after a force close?
How can I stop it happening?
Here is how I start the job, in MainActivity.OnCreate
serviceComponent = new ComponentName(this, Java.Lang.Class.FromType(typeof(ScheduledJob)));
// Start service
var startServiceIntent = new Intent(this, typeof(ScheduledJob));
StartService(startServiceIntent);
var builder = new JobInfo.Builder(1, serviceComponent);
builder.SetPeriodic(App.PollFrequencyMinutes * 60000);
builder.SetRequiredNetworkType(App.WifiOnly ? NetworkType.Unmetered : NetworkType.Any);
var tm = (JobScheduler)GetSystemService(Context.JobSchedulerService);
var status = tm.Schedule(builder.Build());
Here is the job itself:
[Service(Exported = true, Permission = "android.permission.BIND_JOB_SERVICE")]
public class ScheduledJob : JobService {
JobParameters parameters;
public override bool OnStartJob(JobParameters args) {
try {
Utils.Debug("Scheduled job starting");
// New code to see why previous code crashed
if (Xamarin.Forms.Application.Current == null)
Utils.Debug("Application.Current == null");
// End of new code
if (!App.LoggedIn)
return false;
parameters = args;
new Task(doJob).Start();
return true;
} catch(Exception ex) {
Utils.Report(ex);
return false;
}
}
async void doJob() {
Utils.Debug("Scheduled job running");
bool result = await App.Database.SafeSync();
Utils.Debug("Scheduled job complete - done={0}", result);
JobFinished(parameters, !result);
}
public override bool OnStopJob(JobParameters args) {
return true;
}
}
Here is the logcat output for the crash
android.runtime.JavaProxyThrowable: System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it.
at Xamarin.Forms.Device.get_PlatformServices () [0x00007] in <a7f3b60e25304b56ab3b4f0d8c85dcc3>:0
at Xamarin.Forms.Device.GetAssemblies () [0x00000] in <a7f3b60e25304b56ab3b4f0d8c85dcc3>:0
at Xamarin.Forms.DependencyService.Initialize () [0x00008] in <a7f3b60e25304b56ab3b4f0d8c85dcc3>:0
at Xamarin.Forms.DependencyService.Get[T] (Xamarin.Forms.DependencyFetchTarget fetchTarget) [0x00000] in <a7f3b60e25304b56ab3b4f0d8c85dcc3>:0
at DefectReport.App.get_Database () [0x0000e] in <697090e2ae9b42aab8ce5fb8297f8295>:0
at DefectReport.Utils.Report (System.Exception ex) [0x00016] in <697090e2ae9b42aab8ce5fb8297f8295>:0
at DefectReport.Droid.ScheduledJob.OnStartJob (Android.App.Job.JobParameters args) [0x00058] in <697090e2ae9b42aab8ce5fb8297f8295>:0
at Android.App.Job.JobService.n_OnStartJob_Landroid_app_job_JobParameters_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native__params) [0x0000f] in <c82a099136944d8aa96281cf061cbc12>:0
at (wrapper dynamic-method) System.Object:e8eee233-e03f-4af6-8407-39201ccf2589 (intptr,intptr,intptr)
at md5a3827847cc73d14b3bdf7eb5b536dab7.ScheduledJob.n_onStartJob(Native Method)
at md5a3827847cc73d14b3bdf7eb5b536dab7.ScheduledJob.onStartJob(ScheduledJob.java:30)
at android.app.job.JobService$JobHandler.handleMessage(JobService.java:126)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Before I inserted the new code, it crashed in App.LoggedIn - this attempts to read some Properties with code similar to this (simplified for the purposes of this question)
static bool LoggedIn() {
if (Current.Properties.TryGetValue(LoggedIn, out object result) && result != null) {
return Convert.ToBool(result);
}
return false;
}
In this case, the crash log was:
System.NullReferenceException: Object reference not set to an instance of an object
at DefectReport.App.get_LoggedIn () [0x00001] in <697090e2ae9b42aab8ce5fb8297f8295>:0
at DefectReport.Droid.ScheduledJob.OnStartJob (Android.App.Job.JobParameters args) [0x00024] in <697090e2ae9b42aab8ce5fb8297f8295>:0
Upvotes: 1
Views: 368
Reputation: 74174
The Android OS is responsible for instancing/calling JobService
once you schedule it and the OS becomes the client.
If this Android Service
needs to always run, it should be designed to be independent of having a Xamarin.Forms
-based Application init'ed (and also the hosting Activity
).
Otherwise you would need to cancel the future jobs, you could do this in an MainActivity.OnPause
override:
Jobscheduler.CancelAll();
Upvotes: 3