Android ActivityRecognition API, stops running in background

530 views Asked by At

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.

2

There are 2 answers

0
AndroidCoder On BEST ANSWER

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

0
Naman Mittal On

Are you using Intent Service or normal service. Also you have to create a new class and extend it to WakeFulBroadcastReiever and do the same in manifest so that the service as starts as soon as the device boots.