title: Background service stops after application is closed in Xamarin Android
body: <p>I am new to Xamarin development. I have created one background service which is used to track user location. It's working properly till the time application is running/open. As application is closed/destroyed, my background service stops working. I have already spent so much time to find out the reason behind this issue. But bad luck.</p>
  
  <pre><code>[Service]
  public class LocationTrackingService : Service, GoogleApiClient.IConnectionCallbacks,
      GoogleApiClient.IOnConnectionFailedListener, Android.Gms.Location.ILocationListener
  {
      protected const string TAG = "Attendance Frag";
      protected const int REQUEST_CHECK_SETTINGS = 0x1;
      public const long UPDATE_INTERVAL_IN_MILLISECONDS = 20 * 1000;
      public const long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
      protected const string KEY_REQUESTING_LOCATION_UPDATES = "requesting-location-updates";
      protected const string KEY_LOCATION = "location";
      protected const string KEY_LAST_UPDATED_TIME_STRING = "last-updated-time-string";
  
      protected GoogleApiClient mGoogleApiClient;
      protected LocationRequest mLocationRequest;
      protected LocationSettingsRequest mLocationSettingsRequest;
  
  
      public override void OnCreate()
      {
          base.OnCreate();
          BuildGoogleApiClient();
          CreateLocationRequest();
  
          Toast.MakeText(this, "OnCreate", ToastLength.Short).Show();
      }
  
      protected void BuildGoogleApiClient()
      {
          Log.Info("AttendanceFrag", "Building GoogleApiClient");
          mGoogleApiClient = new GoogleApiClient.Builder(this)
              .AddConnectionCallbacks(this)
              .AddOnConnectionFailedListener(this)
              .AddApi(LocationServices.API)
              .Build();
      }
  
      protected void CreateLocationRequest()
      {
          mLocationRequest = new LocationRequest();
          mLocationRequest.SetInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
          mLocationRequest.SetFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
          mLocationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
      }
  
      protected void BuildLocationSettingsRequest()
      {
          LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
          builder.AddLocationRequest(mLocationRequest);
          builder.SetAlwaysShow(true);
          mLocationSettingsRequest = builder.Build();
      }
  
      public override IBinder OnBind(Intent intent)
      {
          return null;
      }
  
      public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
      {
          base.OnStartCommand(intent, flags, startId);
  
          if (mGoogleApiClient != null &amp;&amp; !mGoogleApiClient.IsConnected)
              mGoogleApiClient.Connect();
  
          return StartCommandResult.Sticky;
      }
  
  
      protected async Task StartLocationUpdates()
      {
          await LocationServices.FusedLocationApi.RequestLocationUpdates(
              mGoogleApiClient,
              mLocationRequest,
              this
          );
      }
  
      protected async Task StopLocationUpdates()
      {
          await LocationServices.FusedLocationApi.RemoveLocationUpdates(
                  mGoogleApiClient,
                  this
              );
      }
  
      public async void OnConnected(Bundle connectionHint)
      {
          Log.Info(TAG, "Connected to GoogleApiClient");
  
          await StartLocationUpdates();
      }
  
      public void OnConnectionSuspended(int cause)
      {
          Log.Info(TAG, "Connection suspended");
      }
  
      public void OnConnectionFailed(Android.Gms.Common.ConnectionResult result)
      {
          Log.Info(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.ErrorCode);
      }
  
      public void OnLocationChanged(Location location)
      {
          Toast.MakeText(this, "OnLocationChanged", ToastLength.Short).Show();
  
      }
  
      public override async void OnDestroy()
      {
          base.OnDestroy();
          Toast.MakeText(this, "OnDestroy", ToastLength.Short).Show();
          if (mGoogleApiClient != null
              &amp;&amp; mGoogleApiClient.IsConnected)
          {
              await StopLocationUpdates();
          }
  
          //Start GeoLocation Tracking Service again
          Intent i = new Intent(this, typeof(RestartServiceReceiver));
          i.SetAction(RestartServiceReceiver.START_TRACKING);
          SendBroadcast(i);
  
      }
  }
  </code></pre>
  
  <p>Please, help me</p>