0

I am listening to DetectedActivity, FusedLocation, GoogleFit etc. updates using a pending intent. I pass over

PendingIntent.FLAG_UPDATE_CURRENT when calling: pendingIntent = PendingIntent.getService(this, 12, intentService, PendingIntent.FLAG_UPDATE_CURRENT); 

However, I still get duplicate data.

I tried using a different request id for every time I listen to certain updates (different request id for listening to FusedLocation updates, different request id for listening to GoogleFit updates etc.) but it still gives me duplicate data. I also tried adding different actionIds to the intentService

Inside my Service, I have the following code:

intentService = new Intent(this, SensorsIntentService.class); //in onCreate
pendingIntent = PendingIntent.getService(this, 12, intentService, PendingIntent.FLAG_UPDATE_CURRENT);

I expect only one set of data every time and no duplicates. Any help would be appreciated.

shir
  • 41
  • 7

1 Answers1

0

So, answering my own question, apparently my problem was not about the pending intent flag, but about the onStartCommand inside my service. Apparently it was called twice (second time because of me moving the service to the foreground by calling:

startForeground(id, notification)

(correct me if I am wrong regards to the reason) so the listeners were registering again and so data was being sent twice. Instead of starting this in the onStartCommand I now start the foregroundService in the onBind.

The only thing I didn't figure out is: I had to create a BroadcastReceiver instead of my IntentService to handle the pendingIntent and in my Service implementation I now use

pendingIntent = PendingIntent.getBroadcast(this, 12, intentService, PendingIntent.FLAG_UPDATE_CURRENT); 

instead of

pendingIntent = PendingIntent.getService(this, 12, intentService, PendingIntent.FLAG_UPDATE_CURRENT); 

or else the pendingIntent is never caught. Anyone have an idea why?

shir
  • 41
  • 7