【问题标题】:addProximityAlert and BroadcastReceiversaddProximityAlert 和 BroadcastReceivers
【发布时间】:2017-08-19 19:03:17
【问题描述】:

我目前正在开发一个内置兴趣点的地图应用程序。 这些点应该通过接近警报触发器通知给用户。 这是我正在使用的 addproximityAlert() 代码

loc.addProximityAlert(lat, longe, radius, -1, PendingIntent.getActivity(
            c, 0, new Intent().putExtra(loc_name, loc_name), flag));

这个想法是,一旦警报触发,就会弹出一个警报对话框,其中包含有关该站点的简短说明,可选择关闭警报或获取更多信息(使用 WebView)。

到目前为止,我没有运行时或编译时错误,但是当我接近每个站点时,什么都没有发生。

我关于为什么什么都没有发生的理论是;

1) 我没有正确使用 PendingIntent,或者

2) 我没有正确设置广播接收器

这是广播接收器的 XML 代码,

<receiver android:name=".ProxyAlertReceiver" >
        <intent-filter>
            <action android:name="entering" />
        </intent-filter>
    </receiver>

我目前解决此问题的计划是修改 PendingIntent 以使用这样的新 Intent;

...new Intent(myContext, ProxyAlertReceiver.class)...

看看我是否有任何结果。

非常感谢您对我的问题提出意见和建议!

【问题讨论】:

  • 您是否通过创建一个广播 Intent 的简单测试 Activity 来测试您的 BroadcastReceiver,以查看您的 Intent 过滤器是否设置正确?
  • 是的,尽管我尝试了几种不同类型的广播。我已经在指向 BroadcastReceiver 的自定义 Intent 上尝试了 TIME_TICK、sendBroadcast(尽管我不知道如何完全编写 xml 意图过滤器),并且我也尝试了 PendingIntent 并且也失败了。没有崩溃,但也没有任何反应。

标签: android


【解决方案1】:

你试过 PendingIntent.getBroadcast(...) 吗?

Intent locationReachedIntent = new Intent("entering");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1234, 
    locationReachedIntent, 0);

locationManager.addProximityAlert(longitude, latitude, radius, -1, pendingIntent);

我的应用程序中有上述代码。

【讨论】:

    【解决方案2】:

    使用这个

    Intent locationIntent = new Intent();
    Bundle extras= new Bundle();
    extras.putString("loc_name",loc_name);
    locationIntent.putExtras(extras);
    PendingIntent pendingIntent= new PendingIntent.getActivity(this,0,locationIntent,0);
    loc.addProximityAlert(lat, longe, radius, -1, pendingIntent, flag));
    

    我假设你的 loc_name 是一个字符串。这将起作用。

    【讨论】:

      【解决方案3】:

      实施接近警报不仅仅取决于在Location Manager 上调用addProximity 方法。

      您还必须:

      1. 创建一个接收器类,它会在触发警报时触发并接收状态(进入或退出)和action name*;

        public class ProximityReceiver extends BroadcastReceiver {
         public String TAG ="ProxReceiver";
        
         @Override
         public void onReceive(Context context, Intent intent) {
            /* your code here - sample below */
            final String key = LocationManager.KEY_PROXIMITY_ENTERING;
            final Boolean entering = intent.getBooleanExtra(key, false);
            if (entering) {
              Toast.makeText(context, "LocationReminderReceiver entering", Toast.LENGTH_SHORT).show();
              Log.v(TAG, "Poi entering");
            } else {
              Toast.makeText(context, "LocationReminderReceiver exiting", Toast.LENGTH_SHORT).show();
              Log.v(TAG, "Poi exiting");
            }
            Log.v(TAG,"Poi receive intent["+intent.toString()+"]");
        
            Bundle extras = intent.getExtras();
            // debugging only
            int counterExtras = extras.size();
            if (extras != null) {
              for (String key : extras.keySet()) {
                Object value = extras.get(key);
                Log.d(TAG, "Prox Poi extra "+String.format("key[%s] value[%s] class[%s] count[%s]", key,
                        value.toString(), value.getClass().getName(), String.valueOf(counterExtras)));
              }
            } else {
              Log.v(TAG, "Prox Poi extra empty");
            }
         }
        }
        
      2. 在你的 Manifest 文件中声明这个接收者;

        <receiver android:name=".ProximityReceiver" >
            <intent-filter>
                <action android:name="my" />
            </intent-filter>
        </receiver>
        
      3. 注册(将您的待处理意图关联到)此接收器,添加接近警报。仅在您的代码中注册您的接收器一次。如果一个接收器多次注册,它将为每个接收器实例触发一次(您到达一个 POI,它注册了一个名为 "my" 的挂起意图。**

        // create proximity alert
        Intent locationIntent = new Intent("my");
        ProximityReceiver proximityReceiver = new ProximityReceiver();
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mapView.getContext(), <some identifying text>,
                locationIntent, 0);
        loc.addProximityAlert(lat, longe, radius, -1, PendingIntent.getActivity(
            c, 0, new Intent().putExtra(loc_name, loc_name), flag));
        
        IntentFilter filter = new IntentFilter("my");
        context.registerReceiver(proximityReceiver, filter);
        

      如果在同一活动中运行,context 可以是 this

      除非您想在后台(甚至终止)时继续接收警报,否则您必须在 onPauseonResume 方法中实现接近警报的删除和重新创建,例如 this SO question(跳转至问题结束)。

      note * 在本例中,"my" 将是一个动作的动作名称(参见 Intent 声明),并将与意图和一组附加组件一起传递,其中至少包含键 entering (LocationManager.KEY_PROXIMITY_ENTERING) 带有布尔值,如果一个人正在进入 (1) 或退出 (0) 接近半径,它会为您提供警报的状态。

      注意 ** 如果您已多次为 "my" 注册接收器,它将为每个调用名为 "my" 的意图的接近警报事件触发多次。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-08
        • 2013-12-23
        • 1970-01-01
        • 2010-10-28
        • 2011-07-20
        • 1970-01-01
        相关资源
        最近更新 更多