private PendingIntent GetStopService(Service context)
        {
            var iStopService = new MyIntentBuilder(context).SetCommand((int)Command.Stop).Build();

            var piStopService = PendingIntent.GetService(context, GetRandomNumber(), iStopService, 0);

            return(piStopService);
        }
        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {
            if (MyIntentBuilder.ContainsCommand(intent))
            {
                var command = MyIntentBuilder.GetCommand(intent);

                if (command == (int)HandleNotifications.Command.Stop)
                {
                    ShutdownService();
                    return(StartCommandResult.NotSticky);
                }
            }

            IsRunning = true;

            var hn = new HandleNotifications();

            hn.CreateNotification(this);

            PermissionsContainer.RequestPermissions(this, new[] { Manifest.Permission.AccessFineLocation }).
            ToObservable()
            .Do((p) =>
            {
                var locationPermissionState = p.FirstOrDefault(per => per.Name == Manifest.Permission.AccessFineLocation);

                // if we have the relevant permissions, then setup the sensor monitoring
                if (locationPermissionState != null && locationPermissionState.Granted)
                {
                    SetupSensorMonitoring();
                }
                else
                {
                    // inform the application root that we have missing permissions
                    ApplicationRoot.MissingPermissions(p.Where(permission => !permission.Granted).Select(per => per.Name));
                }
            }).
            Subscribe();

            return(StartCommandResult.Sticky);
        }