Android 广播接收器和多个接近警报出现问题

Android 广播接收器和多个接近警报出现问题,android,android-intent,broadcastreceiver,android-pendingintent,proximity,Android,Android Intent,Broadcastreceiver,Android Pendingintent,Proximity,我意识到这个问题已经存在,但我在实施解决方案时遇到了困难 我用这些问题作为指导: 在我注册接收人的地方: final String NEAR_YOU_INTENT = "neighborhood.crodgers.example.activities.PROXIMITY_ALERT"; IntentFilter filter = new IntentFilter(NEAR_YOU_INTENT); registerReceiver(new LocationReceiver(), filter

我意识到这个问题已经存在,但我在实施解决方案时遇到了困难

我用这些问题作为指导:

在我注册接收人的地方:

final String NEAR_YOU_INTENT = "neighborhood.crodgers.example.activities.PROXIMITY_ALERT";
IntentFilter filter = new IntentFilter(NEAR_YOU_INTENT);
registerReceiver(new LocationReceiver(), filter);
添加接近警报的位置(注意:这是在服务中完成的,因此是上下文抓取):

我还尝试:

intent.setData(""+ requestCode)
(我已经在其他几个地方看到了这一建议)我不再收集所有通知。

问题 问题是您使用了setAction

Intent intent = new Intent(NEAR_YOU_INTENT);
intent.putExtra(ADDRESS, attributes.get(ADDRESS));
intent.setAction(""+requestCode); //HERE IS THE PROBLEM
在最后一行中,您所做的是将操作从接近您的意图更改为任何请求代码。也就是说,你做的相当于

    Intent intent = new Intent();
    intent.setAction(NEAR_YOUR_INTENT);
    intent.setAction(""+requestCode); // THIS OVERWRITES OLD ACTION
额外方法 我怀疑您真正想要做的是将requestCode作为额外的添加到intent中,以便您可以在接收方中检索它。试用

    Intent intent = new Intent(NEAR_YOU_INTENT);
    intent.putExtra(ADDRESS, attributes.get(ADDRESS));
    intent.putExtra("RequestCode", requestCode);
数据方法 或者,您可以将数据设置为您的请求代码。你可以用

    Intent intent = new Intent(NEAR_YOU_INTENT);
    intent.putExtra(ADDRESS, attributes.get(ADDRESS));
    intent.setData("code://" + requestCode);
然后,您需要更改接收器,以便它可以接受“code://” 模式。为此:

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。在国际海事组织,临时措施更容易

问题 问题是您使用了setAction

Intent intent = new Intent(NEAR_YOU_INTENT);
intent.putExtra(ADDRESS, attributes.get(ADDRESS));
intent.setAction(""+requestCode); //HERE IS THE PROBLEM
在最后一行中,您所做的是将操作从接近您的意图更改为任何请求代码。也就是说,你做的相当于

    Intent intent = new Intent();
    intent.setAction(NEAR_YOUR_INTENT);
    intent.setAction(""+requestCode); // THIS OVERWRITES OLD ACTION
额外方法 我怀疑您真正想要做的是将requestCode作为额外的添加到intent中,以便您可以在接收方中检索它。试用

    Intent intent = new Intent(NEAR_YOU_INTENT);
    intent.putExtra(ADDRESS, attributes.get(ADDRESS));
    intent.putExtra("RequestCode", requestCode);
数据方法 或者,您可以将数据设置为您的请求代码。你可以用

    Intent intent = new Intent(NEAR_YOU_INTENT);
    intent.putExtra(ADDRESS, attributes.get(ADDRESS));
    intent.setData("code://" + requestCode);
然后,您需要更改接收器,以便它可以接受“code://” 模式。为此:

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。在国际海事组织,临时措施更容易

谢谢!这个周末我要试一试!谢谢这个周末我要试一试!