Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

How to call PCL service from background service?

$
0
0

Hello,

I have a Xamarin Forms android application.
With with background service that checks every 10 seconds current time and user configs to notify the user that he should drink the medicine.

When I tried to send a message from PeriaodicBackgroundService to PCL

MessagingCenter.Send<string, string>("APP", "PROCESS_TIMING", DateTime.Now.ToString());

it does not get it in PCL.
Is it possible to call the PCL method from the Background Service?

Here is the code of Background Service

 [Service(Name = "com.xamarin.TimestampService",
         Process = ":timestampservice_process",
         Exported = true)]
    class PeriodicBackgroundService : Service
    {
        private const string Tag = "[PeriodicBackgroundService]";

        private bool _isRunning;
        private Context _context;
        private Task _task;

        #region overrides

        public override IBinder OnBind(Intent intent)
        {
            return null;
        }

        public override void OnCreate()
        {
            _context = this;
            _isRunning = false;
            _task = new Task(DoWork);
        }

        public override void OnDestroy()
        {
            _isRunning = false;

            if (_task != null && _task.Status == TaskStatus.RanToCompletion)
            {
                _task.Dispose();
            }
        }

        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            if (!_isRunning)
            {
                _isRunning = true;
                _task.Start();
            }
            return StartCommandResult.Sticky;
        }

        #endregion

        private void DoWork()
        {
            try
            {
                MessagingCenter.Send<string, string>("APP", "PROCESS_TIMING", DateTime.Now.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                StopSelf();
            }
        }
    }

In MainActivity

 public static void SetAlarmForBackgroundServices(Context context)
        {
            var alarmIntent = new Intent(context.ApplicationContext, typeof(AlarmReceiver));
            var broadcast = PendingIntent.GetBroadcast(context.ApplicationContext, 0, alarmIntent, PendingIntentFlags.NoCreate);
            if (broadcast == null)
            {
                var pendingIntent = PendingIntent.GetBroadcast(context.ApplicationContext, 0, alarmIntent, 0);
                var alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
                alarmManager.SetRepeating(AlarmType.ElapsedRealtimeWakeup, SystemClock.ElapsedRealtime(), 10000, pendingIntent);
            }
        }

In App.cs (PCL)

protected override async void OnStart()
        {
       base.OnStart();

            ITranslationService translationService = ViewModelLocator.Resolve<ITranslationService>();
            translationService.LoadLocalizations();

            drugService = ViewModelLocator.Resolve<IDrugService>();
            dependencyService = ViewModelLocator.Resolve<IDependencyService>();

            MessagingCenter.Subscribe<string, string>("APP", "PROCESS_TIMING", async (sender, args) =>
            {
                DrugServiceResult processed = await this.drugService .ProceedTime();

                if (processed.NextDrunk >= DateTime.Now)
                {
                    this.notifiyed = false;
                }
            });

            await InitNavigation();

            base.OnResume();
        }

Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>