private void RefreshJobs() { StopJobs(); if (settings != null && settings.EnableAndroidNotification) { CreateAutoJobNotificationChannel(); var jobServ = (JobScheduler)GetSystemService(JobSchedulerService); if (settings.AutoStopWifi.Count > 0 || settings.AutoStartCellur) { JobInfo.Builder jobBuilder = new JobInfo.Builder( TrojanPlusAutoJobService.JobId, new ComponentName(this, Class.FromType(typeof(TrojanPlusAutoJobService)).Name)); jobBuilder.SetRequiredNetworkType(NetworkType.Any); jobBuilder.SetBackoffCriteria(AutoJobServiceBackoffCriteria, BackoffPolicy.Linear); jobBuilder.SetMinimumLatency(AutoJobServiceMinimumLatency); jobBuilder.SetOverrideDeadline(AutoJobServiceMaxLatency); jobBuilder.SetPersisted(true); PersistableBundle bundle = new PersistableBundle(); bundle.PutString("settings", JsonConvert.SerializeObject(settings)); jobBuilder.SetExtras(bundle); var succ = jobServ.Schedule(jobBuilder.Build()); Log.Debug(TAG, "RefreshJobs TrojanPlusAutoJobService " + succ); } } }
public override void OnUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { System.Diagnostics.Debug.WriteLine("Widget OnUpdate"); var me = new ComponentName(context, Java.Lang.Class.FromType(typeof(AppWidget)).Name); appWidgetManager.UpdateAppWidget(me, BuildRemoteViews(context, appWidgetIds)); var scheduler = context.GetSystemService(Context.JobSchedulerService) as JobScheduler; var jobName = new ComponentName(context, Java.Lang.Class.FromType(typeof(WidgetJobService)).Name); var jobInfo = new JobInfo.Builder(1, jobName); jobInfo.SetBackoffCriteria(5000, BackoffPolicy.Linear); jobInfo.SetPersisted(true); jobInfo.SetPeriodic(900000); jobInfo.SetRequiredNetworkType(NetworkType.Any); jobInfo.SetRequiresCharging(false); var bundle = new PersistableBundle(); bundle.PutIntArray(WIDGET_BUNDLE, appWidgetIds); jobInfo.SetExtras(bundle); var job = jobInfo.Build(); scheduler.Schedule(job); }
public void StartJob() { try { //Supports After APi level 21 only if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop) { return; } ComponentName serviceComponent = new ComponentName(ActivtyContext, Java.Lang.Class.FromType(typeof(DataLoaderJobService))); if (Build.VERSION.SdkInt >= BuildVersionCodes.N) { Jobbuilder = new JobInfo.Builder(Jobid, serviceComponent); Jobbuilder.SetMinimumLatency(4000); Jobbuilder.SetRequiredNetworkType(NetworkType.Any); Jobbuilder.SetRequiresCharging(false); Jobbuilder.SetOverrideDeadline(4000); Jobbuilder.SetBackoffCriteria(3000, BackoffPolicy.Linear); Jobbuilder.SetPersisted(true); } else { Jobbuilder = new JobInfo.Builder(Jobid, serviceComponent); Jobbuilder.SetPeriodic(2000); Jobbuilder.SetPersisted(true); Jobbuilder.SetRequiredNetworkType(NetworkType.Any); Jobbuilder.SetRequiresCharging(false); } Jobscheduler = (JobScheduler)ActivtyContext.GetSystemService(Context.JobSchedulerService); int result = Jobscheduler.Schedule(Jobbuilder.Build()); if (result == JobScheduler.ResultSuccess) { } } catch (Exception e) { Crashes.TrackError(e); } }
private void StartJob(Context context, Intent implicitIntent) { try { this.mJobScheduler = (JobScheduler)this.GetSystemService(Context.JobSchedulerService); this.mJobScheduler.Cancel(this.jobId); if (this.jobId != -1) { this.mJobScheduler.Cancel(this.jobId); } jobId = 100; if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) { ComponentName component = null; PackageManager pm = context.PackageManager; //查出所有的能匹配这个隐式意图的服务列表 var resolveInfo = pm.QueryIntentServices(implicitIntent, 0); if (resolveInfo != null || resolveInfo.Count == 1) { ResolveInfo serviceInfo = resolveInfo[0]; //取出包名 string packageName = serviceInfo.ServiceInfo.PackageName; //取出服务名 string className = serviceInfo.ServiceInfo.Name; //用包名和服务名来创建一个ComponentName对象 component = new ComponentName(packageName, className); } if (component != null) { var builder = new JobInfo.Builder(this.jobId, component); //24 if (Build.VERSION.SdkInt >= BuildVersionCodes.N) { //7.0以上延迟3s执行 builder.SetMinimumLatency(30000L); ////执行的最小延迟时间 builder.SetOverrideDeadline(30000L); ////执行的最长延时时间 builder.SetMinimumLatency(30000L); builder.SetBackoffCriteria(30000L, 0); //线性重试方案 } else { //每隔3s执行一次job builder.SetPeriodic(30000L); } builder.SetRequiredNetworkType(NetworkType.Any); // 当插入充电器,执行该任务 builder.SetRequiresCharging(true); builder.SetPersisted(true); this.mJobScheduler.Schedule(builder.Build()); } } } catch (Java.Lang.Exception ex) { ex.PrintStackTrace(); } }
internal void Apply(JobInfo.Builder builder) { builder .SetBackoffCriteria(_initialBackoffTimeMillis, _backoffPolicy) .SetPersisted(_persisted); }