Beispiel #1
0
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            _cts = new CancellationTokenSource();

            Notification notif = DependencyService.Get <INotification>().ReturnNotif();

            StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notif);

            Task.Run(() => {
                try
                {
                    var locShared = new Location();
                    locShared.Run(_cts.Token).Wait();
                }
                catch (OperationCanceledException)
                {
                }
                finally
                {
                    if (_cts.IsCancellationRequested)
                    {
                        var message = new StopServiceMessage();
                        Device.BeginInvokeOnMainThread(
                            () => MessagingCenter.Send(message, "ServiceStopped")
                            );
                    }
                }
            }, _cts.Token);

            return(StartCommandResult.Sticky);
        }
Beispiel #2
0
        public async Task Start()
        {
            _cts    = new CancellationTokenSource();
            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("com.company.product.name", OnExpiration);

            try
            {
                var locShared = new Location();
                locShared.setRunningStateLocationService(true);
                await locShared.Run(_cts.Token);
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    var message = new StopServiceMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "ServiceStopped")
                        );
                }
            }

            var time = UIApplication.SharedApplication.BackgroundTimeRemaining;

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }
        public async Task Start(int interval)
        {
            _cts    = new CancellationTokenSource();
            _taskId = UIApplication.SharedApplication.BeginBackgroundTask("com.qutgeodev.groundsman", OnExpiration);
            try
            {
                LogService locShared = new LogService();
                await locShared.Run(_cts.Token, interval);
            }
            catch (OperationCanceledException) { }
            finally
            {
                if (_cts.IsCancellationRequested)
                {
                    StopServiceMessage message = new StopServiceMessage();
                    Device.BeginInvokeOnMainThread(
                        () => MessagingCenter.Send(message, "ServiceStopped")
                        );
                }
            }

            double time = UIApplication.SharedApplication.BackgroundTimeRemaining;

            UIApplication.SharedApplication.EndBackgroundTask(_taskId);
        }