I have a service that does database calls. The service receives a request with an intent, and when the database call is complete, it broadcasts an "update complete" intent开发者_StackOverflow社区 indicating the completion of the call.
Sometimes the database is already populated with cached data, in which case I would like to immediately broadcast an "update complete" intent, indicating the activity should display the cached data, and then once the database has been updated fire another "update complete" intent indicating the activity should load the updated data.
The problem is that the second broadcast is never received by the activity. Is this because I'm re-using the same intent object that has already been fired?
Here's the code:
if (scheduleDatabase.populated()) {
intent.putExtra("fromCache", true);
getApplicationContext().sendBroadcast(intent);
}
scheduleDatabase.update();
intent.putExtra("fromCache", false);
getApplicationContext().sendBroadcast(intent);
An update: If I comment out one of the intent broadcasts, the other one always fires and is received. Also, if I create two intent objects with the same action string and fire them separately, only the first one is ever received by the activity. I'm not clear yet on whether the other gets fired but not received, or if it never fires at all.
It turns out that the problems I had with the intents were a symptom of a larger problem in a different area of my code. After fixing that problem, the intents began firing and receiving as I expected. So, to answer my own original question, yes it is possible to fire a single intent multiple times.
I ran into a similar problem. Yes the second intent is overwriting the first one. My workaround was to create a new Intent in my BroadcastReceiver. You have access to the same context, and can pass data in the extras.
精彩评论