public static OneTimeWorkRequest CreateOneTime <TWorker>(Action <OneTimeWorkRequest.Builder> workRequestMutator = null, Action <Constraints.Builder> constraintsMutator = null, Action <Data.Builder> inputData = null) { var requestBuilder = new OneTimeWorkRequest.Builder(typeof(TWorker)); if (inputData != default) { var dataBuilder = new Data.Builder(); inputData.Invoke(dataBuilder); requestBuilder.SetInputData(dataBuilder.Build()); } workRequestMutator?.Invoke(requestBuilder); if (constraintsMutator == null) { requestBuilder.SetConstraints(Constraints.None); } else { var constraintsBuilder = new Constraints.Builder(); constraintsMutator?.Invoke(constraintsBuilder); requestBuilder.SetConstraints(constraintsBuilder.Build()); } return(requestBuilder.Build()); }
public void RegisterPeriodicBackgroundProcess <T>(int repeatMins, BackgroundTaskMetadata metaData) where T : ICoreJob, new() { repeatMins = repeatMins >= 20 ? repeatMins : 20; //This is the minimum var tag = typeof(T).Name; var data = new Data.Builder(); data.Put("activatorDetails", $"{typeof(T).Assembly.GetName().Name},{typeof(T).FullName}"); var constraints = new Constraints.Builder(); if (metaData != null) { constraints.SetRequiresCharging(metaData.RequiresCharging); constraints.SetRequiresBatteryNotLow(metaData.RequiresBatteryNotLow); if (metaData.RequiresNetworkType != null) { if (metaData.RequiresNetworkType == "Connected") { constraints.SetRequiredNetworkType(NetworkType.Connected); } if (metaData.RequiresNetworkType == "Metered") { constraints.SetRequiredNetworkType(NetworkType.Metered); } if (metaData.RequiresNetworkType == "NotRequired") { constraints.SetRequiredNetworkType(NetworkType.NotRequired); } if (metaData.RequiresNetworkType == "NotRoaming") { constraints.SetRequiredNetworkType(NetworkType.NotRoaming); } if (metaData.RequiresNetworkType == "Unmetered") { constraints.SetRequiredNetworkType(NetworkType.Unmetered); } } constraints.SetRequiresDeviceIdle(metaData.RequiresDeviceIdle); constraints.SetRequiresStorageNotLow(metaData.RequiresStorageNotLow); } var pwr = PeriodicWorkRequest.Builder.From <BackgroundTaskWorker>(TimeSpan.FromMinutes(repeatMins)) .SetConstraints(constraints.Build()) .SetInputData(data.Build()) .AddTag(tag) .Build(); WorkManager.Instance.EnqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.Keep, pwr); periodic.Add(tag, pwr.Id); }