【问题标题】:addProximityAlert works initiallyaddProximityAlert 最初工作
【发布时间】:2013-07-08 19:22:51
【问题描述】:

我创建了许多 ProximityAlerts。似乎只有第一个有效,有时第二个有效。它们似乎也过期了。我调用这个方法来创建一个 ProximityAlert:

public boolean addProximityAlert(int id) {

  locationManager.addProximityAlert(
    latitude,
    longitude,
    POINT_RADIUS,             
    PROX_ALERT_EXPIRATION,      
    getPendingIntent(id)    
  );

  registerReceiver(new ProximityIntentReceiver(), getIntentFilter(id));

  return true;
}

private PendingIntent getPendingIntent(int id) {
  Intent intent = new Intent(PROX_ALERT_INTENT + id);
  intent.setAction(String.valueOf(id));
  return PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT);
}

private IntentFilter getIntentFilter(int id) {
    IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT + id);
    filter.addAction(String.valueOf(id));
    return filter;
}

在 ProximityIntentReceiver 中扩展了 BroadcastReceiver:

@Override
public void onReceive(Context context, Intent intent) {
    String key = LocationManager.KEY_PROXIMITY_ENTERING;

    Boolean entering = intent.getBooleanExtra(key, false);

    if (entering) {
        message = context.getString(R.string.notification_alert_entering);
    } else {
        message = context.getString(R.string.notification_alert_exiting);
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

    Notification notification = createNotification();
    notification.setLatestEventInfo(context, context.getString(R.string.notification_alert), message, pendingIntent);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, notification);

}

变量:

private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1;    // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 1000;       // in Milliseconds
private static final long POINT_RADIUS = 500;                       // in Meters
private static final long PROX_ALERT_EXPIRATION = -1;
private static final String PROX_ALERT_INTENT = "com.xxx.yyy.ProximityAlert";

我想创建一些独特的警报,然后在每次进入或退出邻近区域时收到通知(可能发生多次)。所有警报都应该保持有效,有人可以指出正确的方向,为什么这不能正常工作?

【问题讨论】:

  • 你是如何测试的?您是否检查过在进入/退出 POINT_RADIUS 到实际纬度/经度位置时是否发出警报?警报只会在穿过 POINT_RADIUS 时出现。
  • @Shrikant 更新了问题。我使用真实设备进行测试并从该位置进出。 IE。创建警报,将我的车开 2 公里,然后开回原处。
  • 您是否也有良好的互联网连接?因为你的代码对我来说看起来不错。我也做了同样的事情,而且效果很好(我在几乎确切的位置得到警报)。尝试减少 MINIMUM_TIME_BETWEEN_UPDATE(测试设置为 0)
  • 没有让它可靠地工作。它似乎只在本地工作了几次。离开地理点几公里然后回来不会再次触发待处理的Intent?有人能发现代码中的一些错误或其他问题吗?
  • 实际上我的代码有效,问题是需要在您的活动启动或退出广播接收器后添加接近警报和广播接收器。我使用了一个服务来处理它。

标签: android android-intent android-pendingintent android-location


【解决方案1】:

这就是我添加接近度的方式:

String geo = "geo:" + storeProximityData.getLatitude() + ","
                    + storeProximityData.getLongitude();
            Intent intent1 = new Intent(PROXIMITY_ALERT_HOME, Uri.parse(geo));
            PendingIntent proximityIntent = PendingIntent.getBroadcast(
                    sContext.getApplicationContext(), 0, intent1,
                    PendingIntent.FLAG_CANCEL_CURRENT);
            sLocationManager.addProximityAlert(
                    storeProximityData.getLatitude(), // the
                    // latitude
                    // of
                    // the
                    // central
                    // point
                    // of
                    // the alert region
                    storeProximityData.getLongitude(), // the longitude of
                                                        // the
                                                        // central point of
                                                        // the
                                                        // alert
                    // region
                    storeProximityData.getRadius(), // the radius of the
                                                    // central
                                                    // point of the
                                                    // alert
                                                    // region, in
                    // meters
                    expirationTime, // time for this proximity alert, in
                    // milliseconds, or -1 to
                    // indicate no expiration
                    proximityIntent // will be used to generate an Intent to
                                    // fire
                    // when entry to or exit from the alert region
                    // is detected
                    );

这就是我注册接收器的方式:

private static String PROXIMITY_ALERT_HOME = "path.to.ProximityAlert";
IntentFilter filter = new IntentFilter(PROXIMITY_ALERT_HOME);
            filter.addDataScheme("geo");
            sContext.registerReceiver(
                    new ProximityResponderBroadcastReceiver(), filter);

【讨论】:

  • 看来 addProximityAlert 不可靠。不确定我的代码是否错误?
  • 这段代码几乎一直对我有用。但是文档也说它几乎不是每次都有效。可能还有其他问题。文档说:由于位置估计的近似性质,如果设备短暂地通过给定区域,则可能不会触发任何 Intent。同样,如果设备非常接近给定区域但实际上并未进入该区域,则可能会触发 Intent。礼貌:developer.android.com/reference/android/location/…, double, float, long, android.app.PendingIntent)
  • 实际上我的代码有效,问题是需要在您的活动启动或退出广播接收器后添加接近警报和广播接收器。我使用了一个服务来处理它。
  • 是的,没错.. 应该是这样的。您需要添加广播接收器来捕获 BootComplete 意图,并在接收到意图时,检查您是否需要启动邻近...
猜你喜欢
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
相关资源
最近更新 更多