android - addProximityAlert 没有按预期工作

标签 android location locationmanager geofencing android-geofence

ios sdk 具有强大的区域监控功能。我在 android 中需要类似的东西,我认为我们有两种选择。地理围栏和位置管理器。

Geofencing 有非常简洁的示例和错误,所以我更喜欢 LocationManager。 LocationManager 中的一切都很好,除了一个。如果您将当前位置添加为 ProximityAlert ,它会立即触发“ENTERING”,但这是我当前的位置,并不意味着我进入了该区域。因此,如果我在区域中,每次启动我的应用程序时它都会触发“ENTERING”。(即使我没有移动)

我如何解决这个问题并仅在用户真正进入该区域时触发事件?

以下是我如何为我的位置添加 PeddingIntents。

    LocationManager locationManager =  (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);

    for(Place p : places)
    {
        Log.e("location", p.location);

        Bundle extras = new Bundle();
        extras.putString("name", p.displayName);
        extras.putString("id", p.id);
        Intent intent = new Intent(CommandTypes.PROX_ALERT_INTENT);
        intent.putExtra(CommandTypes.PROX_ALERT_INTENT, extras);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext,Integer.parseInt(p.id), intent,PendingIntent.FLAG_CANCEL_CURRENT);
        float radius = 50f;
        locationManager.addProximityAlert(p.lat,
                p.lon, radius, 1000000, pendingIntent);

    }       

接收者

public class ProximityReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    final String key = LocationManager.KEY_PROXIMITY_ENTERING;
    final Boolean entering = intent.getBooleanExtra(key, false);

    Bundle b = intent.getBundleExtra(CommandTypes.PROX_ALERT_INTENT);
    String id = b.getString("id");
    Log.e("here" + id, "here");

    if (entering) {
        Log.e(TAG,"entering");
    } else {
        Log.e(TAG,"leaving");
    }
} 

list

   <receiver android:name=".ProximityReceiver">
        <intent-filter>
            <action android:name="ACTION_PROXIMITY_ALERT" />
        </intent-filter>            
    </receiver>

非常感谢

PS:iOS没有这个问题,他们的文档是这样解释的

在注册授权应用后立即开始监控地理区域。但是,不要期望立即收到事件。只有边界交叉点才会产生事件。因此,如果在注册时用户的位置已经在区域内,则位置管理器不会自动生成事件。相反,您的应用程序必须等待用户跨越区域边界,然后才会生成事件并将其发送给委托(delegate)。也就是说,您可以使用 CLLocationManager 类的 requestStateForRegion: 方法来检查用户是否已经在区域边界内。

最佳答案

编辑:自从我写这篇文章后,地理围栏 API 中添加了一个新东西,“setInitialTrigger”可以缓解这种情况:

https://developers.google.com/android/reference/com/google/android/gms/location/GeofencingRequest.Builder#setInitialTrigger%28int%29

是的,这很麻烦,不幸的是,这是 Android 和 IOS 地理围栏不同的主要点之一。

当您在地理围栏内时,如果 Android 知道您之前在外面或者您在地理围栏内时添加了地理围栏,它会发出警报。

我解决这个问题的方法是在我的广播接收器中设置一个“宽限期”。基本上,当我创建 Geofence 时,我将其创建时间存储在 sharedpreferences 中,并在 onReceive 中检查该值。

通过这样做,任何“即时”命中都将被过滤掉。也许 3 分钟对其他人来说太长了,但根据我在应用程序中使用地理围栏的方式,它对我有用。

private static final Long MIN_PROXALERT_INTERVAL = 18000l; // 3 mins in milliseconds

...

long geofenceCreationTime = session.getPrefs().getCurrentGeofenceCreation();
long elapsedSinceCreation = now - geofenceCreationTime;
if(elapsedSinceCreation < CREATIONTIME_GRACE_PERIOD){
        if (ApplicationSession.DEBUG) {
            Log.d(TAG, "elapsedSinceCreation;"+elapsedSinceCreation+";less than;"+CREATIONTIME_GRACE_PERIOD+";exiting");
        }
        return;

    }

希望你明白我的意思。

希望对您有所帮助。

关于android - addProximityAlert 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20423603/

相关文章:

android - 在 Android 中取消订阅 Azure 通知中心标签

android - 刚刚发布的第一个应用程序。它在我的运行 4.2 的 Nexus 4 上运行良好,但 friend 说它在她运行 4.1 的 Electrify M 摩托罗拉上摇晃时崩溃

ios - 当我在没有数据漫游的国外时,为什么 iOS 会显示 "home"位置?

android - 谷歌地图 - 位置不断在我的真实位置和 0.6 英里外(随机位置)之间跳跃

java - 没有 minDistance 的 Android requestLocationUpdates

java - 位置管理器的 AddGpsStatusListener 在 Android 10 中不起作用

android - RecyclerView 内容落后而不是上面的 TextView

android - Google Play 服务 LocationClient 返回 null

android - isProviderEnabled 始终为网络提供商返回 true

java - 安卓服务未启动