title: Android service won't stop?
body: <p>I want to add a background service to my Xamarin.Forms app to fetch some data from the server every X minutes.. so I added a BroadcastReceiver, a service and an alarm manager to do the job, and the service start successfully with a button click, but when I try to stop the service it won't stop !</p>
  
  <p><strong>this the class to turn on/off the service.</strong></p>
  
  <pre><code>[assembly: Dependency(typeof(Notifications))]
  namespace FleetDesigner.Droid
  {
  class Notifications : Activity, IAndroidNotifications
  {
      Context context = Android.App.Application.Context;
  
      public void StartNotificationsService()
      {
          //Start Notifications Service
          Intent myIntent = new Intent(context, typeof(NotificationsService));
          context.StartService(myIntent);
  
          //Show Snackbar message
          Activity activity = CrossCurrentActivity.Current.Activity;
          Android.Views.View activityRootView = activity.FindViewById(Android.Resource.Id.Content);
          Snackbar.Make(activityRootView, "Notifications activées", Snackbar.LengthLong).Show();
      }
  
      public void StopNotificationsService()
      {
          //Stop Notifications Service
          Intent myIntent = new Intent(context, typeof(NotificationsService));
          context.StopService(myIntent);
  
          //Show Snackbar message
          Activity activity = CrossCurrentActivity.Current.Activity;
          Android.Views.View activityRootView = activity.FindViewById(Android.Resource.Id.Content);
          Snackbar.Make(activityRootView, "Notifications désactivées", Snackbar.LengthLong).Show();
      }
  
  }
  }
  </code></pre>
  
  <p><strong>and this is the service class</strong></p>
  
  <pre><code>namespace FleetDesigner.Droid
  {
  [Service
      (Name = "com.fleet.app.NotificationsService",
       Process = "com.fleet.app.NotificationsService",
       Label = "Service Notifications"
      )]
  
  class NotificationsService : Service
  {
      AlarmManager manager;
      Intent myIntent;
      PendingIntent pendingIntent;
  
      public override IBinder OnBind(Intent intent)
      {
          return null;
      }
  
      //[return: GeneratedEnum]
      public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
      {
          //return base.OnStartCommand(intent, flags, startId);
          manager = (AlarmManager)GetSystemService(Context.AlarmService);
  
          myIntent = new Intent(this, typeof(NotificationsAlarm));
          pendingIntent = PendingIntent.GetBroadcast(this, 0, myIntent, 0);
  
          manager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime() + 1000, 60 * 1000, pendingIntent);
  
  
          return StartCommandResult.NotSticky;
      }
  
      public override void OnDestroy()
      {
          base.OnDestroy();
          pendingIntent.Cancel();
          manager.Cancel(pendingIntent);
      }
  
  }
  }
  </code></pre>
  
  <p><strong>and finally, this is the broadcast receiver (the logic of the service is just to show a local notification)</strong></p>
  
  <pre><code>namespace FleetDesigner.Droid
  {
  [BroadcastReceiver(Enabled = true)]
  public class NotificationsAlarm : BroadcastReceiver
  {
      static Random random = new Random();
      public override void OnReceive(Context context, Intent intent)
      {
          //showNotifications(context, intent);
          int m = random.Next(9999 - 1000) + 1000;
          var title = "Title";
          var channelName = "TestChannel";
          string channelID = "TestChannel";
          string OverSpeedChannelName = "OverSpeedChannel";
  
          string notificationChannel = channelName;
  
          using (var notificationManager = NotificationManager.FromContext(context))
          {
              if (Build.VERSION.SdkInt &gt;= BuildVersionCodes.O)
              {
                  NotificationChannel channel = null;
                  if (channel == null)
                  {
                      channel = new NotificationChannel(channelName, channelName, NotificationImportance.Default)
                      {
                          LockscreenVisibility = NotificationVisibility.Public
                      };
                      channel.SetShowBadge(true);
                      notificationManager.CreateNotificationChannel(channel);
                  }
                  channel.Dispose();
  
                  NotificationChannel overspeedchannel = null;
                  if (overspeedchannel == null)
                  {
                      overspeedchannel = new NotificationChannel(OverSpeedChannelName, OverSpeedChannelName, NotificationImportance.Default)
                      {
                          LockscreenVisibility = NotificationVisibility.Secret
                      };
                      overspeedchannel.SetShowBadge(true);
                      notificationManager.CreateNotificationChannel(overspeedchannel);
                  }
                  overspeedchannel.Dispose();
              }
  
              var notificationBuilder = new NotificationCompat.Builder(context, channelID)
                     .SetContentTitle("Title "+m)
                     .SetContentText("Notification Content")
                     .SetSmallIcon(Resource.Drawable.ic_action_map)
                     .SetShowWhen(false)
                     .SetChannelId(notificationChannel);
  
              var notification = notificationBuilder.Build();
              notificationManager.Notify(m, notification);
          }
      }
  }
  </code></pre>
  
  <p>So, as I said the only problem in this code is.. the notifications (background service) never stops even when I call stopService() !</p>
  
  <p>Any suggestions please ? thanks</p>