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);
        }
        public void RegisterBackgroundProcess <T>() where T : ICoreJob, new()
        {
            var tag  = typeof(T).Name;
            var data = new Data.Builder();

            data.Put("activatorDetails", $"{typeof(T).Assembly.GetName().Name},{typeof(T).FullName}");

            var otwr = OneTimeWorkRequest.Builder.From <BackgroundTaskWorker>()
                       .SetInputData(data.Build())
                       .AddTag(tag)
                       .Build();

            WorkManager.Instance.EnqueueUniqueWork(tag, ExistingWorkPolicy.Keep, otwr);
        }
示例#3
0
        public override async Task Schedule(JobInfo jobInfo)
        {
            await base.Schedule(jobInfo);

            //WorkManager.Initialize(this.context.AppContext, new Configuration())
            var constraints = new Constraints.Builder()
                              .SetRequiresBatteryNotLow(jobInfo.BatteryNotLow)
                              .SetRequiresCharging(jobInfo.DeviceCharging)
                              .SetRequiredNetworkType(ToNative(jobInfo.RequiredInternetAccess))
                              .Build();

            var data = new Data.Builder();

            foreach (var parameter in jobInfo.Parameters)
            {
                data.Put(parameter.Key, parameter.Value);
            }

            if (jobInfo.Repeat)
            {
                var request = PeriodicWorkRequest
                              .Builder
                              .From <ShinyJobWorker>(TimeSpan.FromMinutes(20))
                              .SetConstraints(constraints)
                              .SetInputData(data.Build())
                              .Build();

                WorkManager.Instance.EnqueueUniquePeriodicWork(
                    jobInfo.Identifier,
                    ExistingPeriodicWorkPolicy.Replace,
                    request
                    );
            }
            else
            {
                var worker = new OneTimeWorkRequest.Builder()
                             .SetInputData(data.Build())
                             .SetConstraints(constraints);
            }
        }