1

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.

M Vignesh
  • 1,586
  • 2
  • 18
  • 55

1 Answers1

0

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);
}
abhi
  • 1,412
  • 19
  • 25
  • When the app quits I dont have the instance of private UserActivityDetectionRemover mUserActivityDetectionRemover; This could be null. So am not able to call remove. When the app is open I was able to remove properly. – M Vignesh Dec 18 '14 at 12:43
  • But why don't you call this from onDestroy? So before your app is destroyed the updates will be removed. – abhi Dec 18 '14 at 12:46
  • My requirement is I want to track the user activity even the app is not running. It still continue to run even then app quits. – M Vignesh Dec 18 '14 at 12:52
  • but if the app is killed how can you track the user activity? to me it seems you design is not geed because if you can trace user activity that means there are objects in the memory for your app and so you can even remove the updates in that case. – abhi Dec 18 '14 at 13:20
  • Android the intent service will get fired even the app is quit. Request the user activity with pending intent. This call send you the user activity information at the specified interval to your intent service even the app is quit. – M Vignesh Dec 19 '14 at 06:34
  • Can you not create the ibject again by using the same pending intent and by using PendingIntent.FLAG_UPDATE_CURRENT? – abhi Dec 19 '14 at 11:22
  • I have to call removeActivityUpdates(), and I need to disconnect, then I have to cancel the pending intent. – M Vignesh Dec 19 '14 at 12:07