我有一个Xamarin应用程序,它运行一个重复的ScheduledJob。
如果应用程序强行关闭,则下次运行预定作业时,应用程序崩溃。
为什么在强制关闭后仍然会运行预定作业?
如何阻止它发生?
以下是我在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());
这是工作本身:
[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;
}
}
这是崩溃的logcat输出
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)
在我插入新代码之前,它在App.LoggedIn中崩溃了 - 这会尝试使用与此类似的代码读取一些属性(为了这个问题而简化)
static bool LoggedIn() {
if (Current.Properties.TryGetValue(LoggedIn, out object result) && result != null) {
return Convert.ToBool(result);
}
return false;
}
在这种情况下,崩溃日志是:
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
答案 0 :(得分:2)
一旦您安排并且操作系统成为客户端,Android操作系统负责实例化/调用JobService
。
如果此Android Service
需要始终运行,则应将其设计为独立于基于Xamarin.Forms
的应用初始化(以及托管Activity
)
否则您需要取消以后的工作,您可以在MainActivity.OnPause
覆盖中执行此操作:
Jobscheduler.CancelAll();