title: Android ActivityRecognition API, stops running in background
body: <p>I'm dealing with the Activity Recognition API, trying to make it run in the background. 
  So I've made a service that connects to Play Services and is alive all the time. I'm throwing a toast when the service is destroyed and created again. The problem is that after a while, even when the service is running, it stops detecting activities.</p>
  
  <p>Here is some of the code from the service.</p>
  
  <pre><code>@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
      Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      buildGoogleClient();
      return START_STICKY;
  }
  
  private void buildGoogleClient()
  {
      mGoogleApiClientActivityDetection = new GoogleApiClient.Builder(this)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .addApi(ActivityRecognition.API)
              .build();
      mGoogleApiClientActivityDetection.connect();
  
  }
  
  @Override
  public void onConnected(Bundle bundle)
  {
      if (mGoogleApiClientActivityDetection.isConnected() &amp;&amp; detectionConnectionSet == false)
      {
          detectionConnectionSet = true;
          ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
                  mGoogleApiClientActivityDetection,
                  Constants.ACTIVITY_DETECTION_INTERVAL_IN_MILLISECONDS,
                  PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
      }
  }
  </code></pre>
  
  <p>I also override the onConnectionSuspended &amp; onConnectionFailed where I reconnect if it not connected.</p>
  
  <p>This code works fine for some time, then it suddenly just stops.
  Anyone have some ideas to why?</p>
  
  <p>Thanks.</p>