【发布时间】:2014-11-03 07:45:50
【问题描述】:
我使用了带有 Geofences 的 Android 教程,很明显,当应用程序关闭时它不起作用。因此,在四处搜索之后,我注意到用户 b-ryce 在 SO 上使用了 BroadcastReceiver,因此即使应用程序未处于活动状态,也会触发地理围栏 (link for his SO question)。
当我移出/移入注册位置时,我无法强制触发地理围栏。这是我的程序:
/**
* GeoLocation library
*/
mGeoLocation = new GeoLocation(sInstance);
/**
* mReceiver init
*/
mReceiver = new Receiver();
sInstance.registerReceiver(mReceiver, mReceiver.createIntentFilter());
在 GeoLocation 类中:
/*
* Create a PendingIntent that triggers an IntentService in your
* app when a geofence transition occurs.
*/
protected PendingIntent getTransitionPendingIntent() {
if (mGeoPendingIntent != null) {
return mGeoPendingIntent;
}
else {
// Create an explicit Intent
// Intent intent = new Intent(mContext,
// ReceiveTransitionsIntentService.class);
Intent intent = new Intent(getClass().getPackage().getName() + ".GEOFENCE_RECEIVE");
/**
* Return the PendingIntent
*/
return PendingIntent.getBroadcast(
mContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
}
}
以下是我创建新地理围栏的方法:
Geofence fence = new Geofence.Builder()
.setRequestId(hashCode)
// when entering this geofence
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
.setCircularRegion(
Double.parseDouble(single.getSetting("latitude")),
Double.parseDouble(single.getSetting("longitude")),
Float.parseFloat(single.getSetting("radius")) // radius in meters
)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
mGeofences.add(fence);
数组被填充,我们在 Geofence 类中调用 AddGeofences 方法
mGeoLocation.AddGeofences(mGeofences);
接收者类的AndroidManifest.xml:
<!-- RECEIVER -->
<receiver android:name=".Receiver" >
</receiver>
接收器类应该只在地理围栏触发时记录
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d("sfen", "Broadcast recieved "+ action +" but not needed.");
}
问题是,只有当我在我的应用程序中打开我选择位置的地图时,才会触发地理围栏。关闭应用(在后台运行)不会触发任何事情,地理围栏转换也不会触发任何事情。
谁能告诉我我做错了什么?
【问题讨论】:
-
当您打开另一个使用地理定位的应用程序时,地理围栏是否会触发?例如,如果您设置了地理围栏,请关闭您的应用程序,然后打开 Google 地图并移入或移出地理围栏区域。这会触发地理围栏吗?
-
@RussWilde - 是的,当我在后台移动我的应用并打开 Google 地图时,我的两个操作都会被触发。
-
这很不幸;我对地理围栏的结果非常复杂,包括在 IFTTT 和 Field Trip 等其他应用程序中,每次我想知道为什么一些地理围栏没有可靠地触发并且我猜到 Android 的底层位置服务只是没有更新经常足以捕捉到每个位置。遗憾的是,如果是这种情况,我没有有用的解决方案,但我也对其他人可以提供的任何答案感兴趣。
-
@gregor - 嗨,你介意分享一下你最终是如何完成这项工作的吗?
-
@Rat-a-tat-a-tatRatatouille 直到今天,我都找不到合适的解决方案。联系谷歌位置服务开发商也没有工作(因为没有人回复)。
标签: android geolocation android-pendingintent android-geofence