与广播接收器和多个接近警报问题 [英] Problems with BroadcastReceiver and multiple proximity alerts
问题描述
我意识到这个问题已经存在,但我有落实解决的麻烦。
I realize this question already exists, but I am having trouble implementing the solutions.
我用这些问题作为guidlines:
I'm using these questions as guidlines:
<一个href=\"http://stackoverflow.com/questions/5286108/multiple-proximity-alert-based-on-a-service\">multiple基于服务接近警戒
我在哪里注册的接收器:
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);
我也试过:
intent.setData(""+ requestCode)
(我看到这个建议在其他几个地方)我停下来的通知,都在一起。
(I have seen this recommended in several other places) I stopped getting notifications all together.
推荐答案
问题是,您使用的setAction
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
你最终在最后一行做的是改变从NEAR_YOUR_INTENT到任何请求,code正好是行动。 IE浏览器,你正在做的相当于
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
附加方法
我怀疑你真正想要做的是作为一个额外的添加请求code的意图,这样就可以在你的接收器检索。尝试使用
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);
数据方法
另外,你可以设置数据您的要求code。 IE浏览器,你可以使用
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);
然后,你需要改变你的接收器,然后以便它可以接受code://
架构。要做到这一点:
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);
那么你很可能使用一些方法,当你得到你的意图,从数据字段解析出ID。海事组织,演员的做法是更容易。
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.
这篇关于与广播接收器和多个接近警报问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!