问题
I realize this question already exists, but I am having trouble implementing the solutions.
I'm using these questions as guidlines:
multiple proximity alert based on a service
set 2 proximity alerts with the same broadcast
Where I register the receiver:
final String NEAR_YOU_INTENT = "neighborhood.crodgers.example.activities.PROXIMITY_ALERT";
IntentFilter filter = new IntentFilter(NEAR_YOU_INTENT);
registerReceiver(new LocationReceiver(), filter);
Where the proximity alerts are added (Note: This is done in a service, hence the context grab):
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final String NEAR_YOU_INTENT = "neighborhood.crodgers.example.activities.PROXIMITY_ALERT";
Context context = getApplication().getApplicationContext();
int requestCode = 12345; //Spaceballs, anyone? anyone?
for (String domain : domainNames)
{
String[] itemNames = itemGetter();
for (String item : itemNames)
{
HashMap<String, String> attributes = getAttributesForItem(domain, item);
Intent intent = new Intent(NEAR_YOU_INTENT);
intent.putExtra(ADDRESS, attributes.get(ADDRESS));
intent.setAction(""+requestCode);
PendingIntent proximity = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
manager.addProximityAlert(Double.parseDouble(attributes.get(LATITUDE)),
Double.parseDouble(attributes.get(LONGITUDE)),
6000f, -1, proximity);
requestCode++;
}
}
Originally, I was getting notified for the first proximity alert added (using notifications from the receiver). After adding
intent.setAction(""+requestCode);
I also tried:
intent.setData(""+ requestCode)
(I have seen this recommended in several other places) I stopped getting notifications all together.
回答1:
Problem
The problem is that you use setAction
Intent intent = new Intent(NEAR_YOU_INTENT);
intent.putExtra(ADDRESS, attributes.get(ADDRESS));
intent.setAction(""+requestCode); //HERE IS THE PROBLEM
What you end up doing in that last line is changing the action from NEAR_YOUR_INTENT to whatever request code happens to be. IE, you are doing the equivalent of
Intent intent = new Intent();
intent.setAction(NEAR_YOUR_INTENT);
intent.setAction(""+requestCode); // THIS OVERWRITES OLD ACTION
Extra Approach
I suspect what you really want to do is add the requestCode as an extra to the intent so that you can retrieve it in your receiver. Try using
Intent intent = new Intent(NEAR_YOU_INTENT);
intent.putExtra(ADDRESS, attributes.get(ADDRESS));
intent.putExtra("RequestCode", requestCode);
Data Approach
Alternatively, you could set the data to be your request code. IE, you could use
Intent intent = new Intent(NEAR_YOU_INTENT);
intent.putExtra(ADDRESS, attributes.get(ADDRESS));
intent.setData("code://" + requestCode);
Then you would need to alter your receiver then so that it can accept the "code://" schema. To do this:
final String NEAR_YOU_INTENT = "neighborhood.crodgers.example.activities.PROXIMITY_ALERT";
IntentFilter filter = new IntentFilter(NEAR_YOU_INTENT);
filter.addDataScheme("code");
registerReceiver(new LocationReceiver(), filter);
Then you could probably use some method to parse out the id from the data field when you get your intent. IMO, the extras approach is easier.
来源:https://stackoverflow.com/questions/8358907/problems-with-broadcastreceiver-and-multiple-proximity-alerts