androidservicebackgroundgoogle-play-servicesactivity-recognition

Android ActivityRecognition API, stops running in background


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.

Here is some of the code from the service.

@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() && detectionConnectionSet == false)
    {
        detectionConnectionSet = true;
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
                mGoogleApiClientActivityDetection,
                Constants.ACTIVITY_DETECTION_INTERVAL_IN_MILLISECONDS,
                PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
    }
}

I also override the onConnectionSuspended & onConnectionFailed where I reconnect if it not connected.

This code works fine for some time, then it suddenly just stops. Anyone have some ideas to why?

Thanks.


Solution

  • Turns out it was aggressive task management of some phone vendors.