android - addProximityAlert 最初有效

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

我创建了许多 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";

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

最佳答案

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

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);

关于android - addProximityAlert 最初有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17577836/

相关文章:

java - 减少 Firestore Android 中的读取次数

android - 类别主页和类别启动器之间的区别

android - 如何在不使用 textview 的情况下从 Hashmap 获取一个值到 Intent ?

android - 两个按钮,一个 Activity

android - 使用 Intent 请求获取用户编号不起作用?

android - 提取通知待定 Intent 内容以实现辅助功能

java - 序列化循环对象树 - StackOverflowError - 需要自定义序列化代码

android - 在不重置预览或停止 MediaRecorder 的情况下录制视频

android - 如何在 AsyncTask 的弹出窗口中打开 WebView

Android 重复闹钟没有正确重复