Skip to content Skip to sidebar Skip to footer

Problems With BroadcastReceiver And Multiple Proximity Alerts

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

Solution 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.


Post a Comment for "Problems With BroadcastReceiver And Multiple Proximity Alerts"