title: Best way to run a never ending service in Android
body: <p>I have a service that gives a notification if user has changed his location. I want this service to keep on running until user explicitly force stops my application in application manager. I have used following method:</p>
  
  <pre><code>        Intent intent1 = new Intent(context, LocationService2.class);
          PendingIntent contentIntent = PendingIntent.getService(context, 0, intent1, 0);
          AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
          am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),2*60000, contentIntent);
  </code></pre>
  
  <p>Service class: </p>
  
  <pre><code>public class LocationService2 extends Service implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
  private GoogleApiClient mGoogleApiClient;
  
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Log.v("TAG", "STARTLS");
      mGoogleApiClient = new GoogleApiClient.Builder(this)
                  .addConnectionCallbacks(this)
                  .addOnConnectionFailedListener(this)
                  .addApi(LocationServices.API)
                  .build();
  
      mGoogleApiClient.connect();
      return START_STICKY;
  }
  
  @Override
  public void onConnected(Bundle bundle) {
      Log.i(TAG, "Location services connected.");
  
      Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
      // Use this location to give notification if required.
  }
  
  @Override
  public void onConnectionSuspended(int i) {
      Log.i(TAG, "Location services suspended. Please reconnect.");
  }
  
  @Override
  public void onConnectionFailed(ConnectionResult connectionResult) {
  
  }
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      mGoogleApiClient.disconnect();
  }
  }
  </code></pre>
  
  <p>This method does not work on all phones.
  Is AlarmManager the best way to do this. If yes, then how can I improve this code to work on all phones?</p>