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);
        }
示例#2
0
        public void RegisterPeriodicBackgroundProcess <T>(int repeatMins, BackgroundTaskMetadata metaData) where T : ICoreJob, new()
        {
            if (HasBackgroundMode("fetch"))
            {
                BackgroundTask.Interval = repeatMins <= 15 ? UIApplication.BackgroundFetchIntervalMinimum : (repeatMins * 60);
                UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(BackgroundTask.Interval);

                var json = $"{typeof(T).Assembly.GetName().Name},{typeof(T).FullName}";

                var invoker = (ICoreJob)Activator.CreateInstance(typeof(T).Assembly.GetName().Name, typeof(T).FullName).Unwrap();
                new BackgroundExecutionService().Start(invoker);

                AddSetting(typeof(T).Name, json);
            }
        }