0

I want to monitor location changes using FusedLocationProviderApi and phone activity changes using ActivityRecognitionApi in the background, even if I close my app.

IntentService handles PendingIntents and does some long running operations (around 10 seconds). Code below is requesting location and activity recognition updates, every change calls SenseDataIntentService.

I am calling methods requestLocationUpdates() and requestActivityUpdates() on every app start and never stop them as I want updates constantly in background. The problem occurs when I start the app too many times and the IntentService queue gets big (I get same location and phone activities multiple times). I thought that PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_UPDATE_CURRENT would solve the issue but this is not the case.

Any solutions to this problem? Like how to stop all previously requested location updates by this app and only then starting new request. Any help will be much appreciated!

Please find the relevant code pieces below.

private void requestLocationUpdates() {
    if (!PermissionsHelper.hasPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) {
        Log.d(TAG, "No location permissions provided.");
        mPendingAction = true;
        return;
    }
    stopAllUpdates();
    Log.d(TAG, "Firing requested location updates.");
    LocationRequest myLocationRequest = new LocationRequest();
    myLocationRequest.setInterval(Constants.APPROXIMATE_INTERVAL_MILLIS);
    myLocationRequest.setPriority(LocationRequest.PRIORITY_NO_POWER);
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, myLocationRequest, getSensingServicePendingIntent());
}

Method to build PendingIntent:

private PendingIntent getSensingServicePendingIntent(Integer userLabel) {
    Intent i = new Intent(mContext, SenseDataIntentService.class);
    return PendingIntent
            .getService(mContext, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
}

Code for requesting phone activity changes:

private void requestActivityUpdates() {
    Log.d(TAG, "Starting requested activity recognition updates.");
    stopAllUpdates();
    ActivityRecognition
            .ActivityRecognitionApi
            .requestActivityUpdates(mGoogleApiClient, Constants.APPROXIMATE_INTERVAL_MILLIS, getSensingServicePendingIntent())
            .setResultCallback(this);
}
urgas9
  • 806
  • 9
  • 22

2 Answers2

0

Before you can request location updates, your app must be connected to location services and make a location request. Once a location request is in place you can start the regular updates by calling requestLocationUpdates(). Do this in the onConnected() callback provided by Google API Client, which is called when the client is ready.

This page shows you how to request regular updates about a device's location using the requestLocationUpdates() method in the fused location provider.

About the requestActivityUpdates. Activities may be received more frequently than the detectionIntervalMillis parameter if another application has also requested activity updates at a faster rate. It may also receive updates faster when the activity detection service receives a signal that the current activity may change, such as if the device has been still for a long period of time and is then unplugged from a phone charger.

To Stop the location update you can call the onPause() method. To stop location updates, call removeLocationUpdates(), passing it your instance of the GoogleApiClient object and a LocationListener, as shown in the following code sample:

@Override
protected void onPause() {
    super.onPause();
    stopLocationUpdates();
}
protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Thanks for your answer, but it does not address my problem. I want to monitor location updates in the background, even when I close the app. Maybe I didn't state this clearly in my question, will edit it. – urgas9 Mar 15 '16 at 12:37
0

Use Service instead of IntentService. FusedLocation work in background so, It will not takes much resources.

If you are going to work heavy after getting updates, do it in another thread. Service will restart on each call, if it is already running. So, no need to worry about queue IntentService.

Lalit Jadav
  • 1,409
  • 1
  • 15
  • 38