How to stop the activity recognition after the quitting the application?

1k Views Asked by At

I am working on a user activity tracking application. I need to track the user activity even the app is background or quits.

I have started the user activity tracking and quitting the app. After some point of time I want to stop the user activity tracking. Now the app is not having any instance. I don't have any instance to stop the user activity tracking.

After I remove the app from my recent application All the static references are cleared. Everything works fine. But I don't know How to stop the user activity tracking some time after the app quits.

/**
 * Make the actual update request. This is called from onConnected().
 */
private void continueRequestActivityUpdates() {
    /*
     * Request updates, using the default detection interval. The
     * PendingIntent sends updates to ActivityRecognitionIntentService
     */
    getActivityRecognitionClient().requestActivityUpdates(
            Constants.DETECTION_INTERVAL_MILLISECONDS,
            createRequestPendingIntent());

}

I want to know how to stop this when I was in background. Because once the app quits I don't have any instance to stop it.

Please help me on this. Thanks in advance.

1

There are 1 best solutions below

7
On

To stop activity recognition updates, use the same pattern you used to request updates, but call removeActivityUpdates() instead of requestActivityUpdates(). Took this from here: link

Implement this method in the same class and call this whenever you want to remove updates:

private void removeActivityUpdates() {
    mActivityRecognitionClient = new ActivityRecognitionClient(mContext, this, this);

    Intent intent = new Intent(mContext, ActivityRecognitionIntentService.class);

    mActivityRecognitionPendingIntent = PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mActivityRecognitionClient.removeActivityUpdates(mActivityRecognitionPendingIntent);
}