我正在尝试实施Geofencing机制,其中监控地理围栏并且一旦用户退出当前地理围栏,当前坐标用于创建新的地理围栏并且启动db查询以获取一些数据。
我的问题是,挂起的意图永远不会被解雇。
从日志中我可以看到地理围栏正被添加到位置客户端。然而,在位置变化时没有触发任何未决的意图。(我已经将围栏半径设置为2米并且我走过了超过100米)。我宣布意图服务的方式有问题吗?
这是意向服务类。
public class GeoFenceIntentService extends IntentService{
private static final String mIntentName = "GeoFenceIntentService";
public GeoFenceIntentService() {
super(mIntentName);
}
@Override
protected void onHandleIntent(Intent intent) {
int transitionType = LocationClient.getGeofenceTransition(intent);
Log.e(TAG,"Inside fence handler");
if(transitionType == Geofence.GEOFENCE_TRANSITION_EXIT){
//Query DB here with current co-ords
//create new GeoFence
Location location = LocationHelper.getInstance(mContext).getLastLocation();
mLat = String.valueOf(location.getLatitude());
mLong = String.valueOf(location.getLongitude());
addGeofenceToMonitor(location);
queryDb();
}
}
}
此外,我还将挂起的意图和地理围栏添加到位置客户端
addGeofenceToMonitor(Location location){
List<Geofence> list = new ArrayList<Geofence>();
list.add(getNewGeofence(location.getLatitude(), location.getLongitude()));
PendingIntent pendingIntent = PendingIntent.getService(mContext, 0,
new Intent(mContext,GeoFenceIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
OnRemoveGeofencesResultListener removeListener = new OnRemoveGeofencesResultListener() {
@Override
public void onRemoveGeofencesByRequestIdsResult(int statusCode, String[] requestIDs) {
//To be used
}
@Override
public void onRemoveGeofencesByPendingIntentResult(int statusCode,PendingIntent pendingIntent) {
//Not used
}
};
LocationHelper.getInstance(mContext).removeGeoFence(mGeofenceRequestIDs, removeListener);
OnAddGeofencesResultListener addListener = new OnAddGeofencesResultListener() {
@Override
public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
if(statusCode != LocationStatusCodes.SUCCESS){
//handle error cases
}
else
Log.i(TAG, "Successfully added Geofence "+geofenceRequestIds[0]+" for monitoring");
}
};
LocationHelper.getInstance(mContext).addGeoFence(list, pendingIntent, addListener);
}
以下是清单文件
中的代码段<service
android:name="com.myexample.sample.GeoFenceIntentService"
android:label="@string/app_name"
android:exported="true">
</service>
答案 0 :(得分:0)
Read this。 你检查了你得到的位置估算圈吗?您可以使用模拟位置应用程序来设置位置和精度圆。您的地理围栏可能太小而无法容纳您的位置圈,这就是未触发事件的原因。
答案 1 :(得分:0)
Android GeoFences永远不会启用GPS(因为他们的API非常糟糕,他们的设备功耗已经失控)。如果您想通过GPS进行地理围栏,则必须设置地理围栏,然后不断轮询GPS。
GPS轮询的处理程序可以为空,仅仅存在轮询以将准确的信息强制到其糟糕的位置API中,从而触发围栏。