Send Update to Repeating BroadcastReceiver

423 views Asked by At

I have something like the following for initiating an IntentService via repeated calls to a BroadcastReceiver to poll for server updates:

AlarmManager pollManager;
Intent pollIntent;
PendingIntent pollPendingIntent;

...

pollIntent = new Intent(getActivity(), ActionUpdateReceiver.class);
pollIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pollIntent.putExtra(RECEIVER, resultReceiver);
pollIntent.putExtra(USER, accountId);

// This is the crux of my question
pollIntent.putExtra(SOMETHING_THAT_UPDATES, updatingThing);

pollPendingIntent = PendingIntent.getBroadcast(getActivity(), ACTION_REQUEST,
        pollIntent, PendingIntent.FLAG_CANCEL_CURRENT);
pollManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
        POLL_INTERVAL, pollPendingIntent);

In terms of polling the server and using a ResultReceiver for obtaining server updates, the above approach works well. However, I need to provide some feedback to the polling service in order to alter my update queries.

How should I provide feedback to the polling service? If updated queries are called for, do I just need to cancel the current alarm and set-up the intents again? Is there a better approach than canceling?

1

There are 1 answers

1
Shehab ElDin On BEST ANSWER

I think that your problem ... I think you want to change the value on "updatingThing" somewhere in your project after the alarm manager setup above if this is your problem check this solution for setup alarm in the code above just replace this line

pollIntent = new Intent(getActivity(), ActionUpdateReceiver.class);

by this

pollIntent = new Intent(getActivity().getApplicationContext(), ActionUpdateReceiver.class);

and for PendingIntent also

pollPendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), ACTION_REQUEST,
        pollIntent, PendingIntent.FLAG_CANCEL_CURRENT);

and put this code where you want to feedback your update

AlarmManager pollManager;
Intent pollIntent;
PendingIntent pollPendingIntent;

...

pollIntent = new Intent(getActivity().getApplicationContext(), ActionUpdateReceiver.class);
pollIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
pollIntent.putExtra(RECEIVER, resultReceiver);
pollIntent.putExtra(USER, accountId);


pollIntent.putExtra(SOMETHING_THAT_UPDATES, updatingThing); // your update here

pollPendingIntent = PendingIntent.getBroadcast(getActivity().getApplicationContext(), ACTION_REQUEST,
        pollIntent, PendingIntent.FLAG_CANCEL_CURRENT);
pollManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
        POLL_INTERVAL, pollPendingIntent);