コード例 #1
0
        private async Task StartInBackground()
        {
            try
            {
                _backgroundService = _backgroundServiceCreationFunc();
                await _backgroundService.StartAsync();

                using (var powerService = ApplicationContext.GetSystemService(PowerService) as PowerManager)
                {
                    if (powerService == null)
                    {
                        return;
                    }
                    _wakeLock = powerService.NewWakeLock(WakeLockFlags.Partial, _serviceName);
                }

                AcquireWakelock();
                if (_hasPeriodicTask)
                {
                    StartHandlerThread();
                }
            }
            catch (Exception e)
            {
                Android.Util.Log.Error(_serviceName, e.ToString());
            }
        }
        /// <inheritdoc />
        public override void OnCreate()
        {
            base.OnCreate();
            _actionMainActivity = $"{Application.PackageName}.show_main_activity";

            _notificationManager = GetSystemService(NotificationService) as NotificationManager;
            if (_notificationManager == null)
            {
                throw new Exception("Unable to get NotificationManager in NativeBackgroundService");
            }

            // Create notification channel
            if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
            {
                var notificationChannel = new NotificationChannel(_builder.ChannelId,
                                                                  _builder.ChannelName, _builder.NotificationImportance)
                {
                    LockscreenVisibility = _builder.NotificationVisibility
                };
                _notificationManager.CreateNotificationChannel(notificationChannel);
            }

            _backgroundService = _builder.Build();
            var periodicServices = _backgroundService.GetPeriodicServices();

            foreach (var(periodicService, period) in periodicServices)
            {
                _backgroundServiceTasks.Add(new BackgroundServiceTask(periodicService, period));
            }
        }
コード例 #3
0
        private async Task BackgroundTaskAsync()
        {
            try
            {
                _backgroundService = _backgroundServiceCreationFunc();
                await _backgroundService.StartAsync();

                _messagingCenter.Send <object, BackgroundServiceState>(this,
                                                                       FromBackgroundMessages.BackgroundServiceState, new BackgroundServiceState(true));
                while (!_cancellationTokenSource.IsCancellationRequested)
                {
                    try
                    {
                        await _backgroundService.PeriodicTaskAsync();

                        if (!_cancellationTokenSource.IsCancellationRequested)
                        {
                            await Task.Delay(_backgroundService.PeriodicServiceCallInterval, _cancellationTokenSource.Token);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }
コード例 #4
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <exception cref="InvalidOperationException"></exception>
 public NativeBackgroundServiceHost()
 {
     _messagingCenter = MessagingCenter.Instance;
     if (_backgroundServiceHostBuilder == null)
     {
         throw new InvalidOperationException("You must call NativeBackgroundServiceHost.Init() before instantiate it");
     }
     _backgroundService = _backgroundServiceHostBuilder.Build();
 }