Android 未激发未决意图
我正在尝试实现一种地理围栏机制,其中地理围栏受到监控,一旦用户退出当前地理围栏,当前坐标将用于创建新的地理围栏,并启动db查询以获取一些数据 我的问题是,悬而未决的意图从未被激发 从日志中,我可以看到GeoFence正在添加到location客户端。然而,在位置改变时,不会有任何悬而未决的意图(我已经将围栏半径设置为2米,我已经走了100米)。我声明意向服务的方式是否有问题 这里是intent服务类Android 未激发未决意图,android,android-intent,android-pendingintent,android-geofence,Android,Android Intent,Android Pendingintent,Android Geofence,我正在尝试实现一种地理围栏机制,其中地理围栏受到监控,一旦用户退出当前地理围栏,当前坐标将用于创建新的地理围栏,并启动db查询以获取一些数据 我的问题是,悬而未决的意图从未被激发 从日志中,我可以看到GeoFence正在添加到location客户端。然而,在位置改变时,不会有任何悬而未决的意图(我已经将围栏半径设置为2米,我已经走了100米)。我声明意向服务的方式是否有问题 这里是intent服务类 public class GeoFenceIntentService extends Inten
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();
}
}
}
在这里,我将挂起的意图和地理围栏添加到location客户端
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);
}
addGeofenceToMonitor(位置){
列表=新的ArrayList();
add(getNewGeofence(location.getLatitude(),location.getLatitude());
PendingEvent PendingEvent=PendingEvent.getService(mContext,0,
新意图(mContext、geofinententservice.class)、pendingent.FLAG_UPDATE_CURRENT);
OnRemoveGeofencesResultListener removeListener=新建OnRemoveGeofencesResultListener(){
@凌驾
public void onRemoveGeoFencesByRequestsResult(int状态码,字符串[]RequestId){
//使用
}
@凌驾
RemoveGeoFencesByEndingIntentResult(int状态代码,PendingIntent PendingIntent)上的公共无效{
//不用
}
};
LocationHelper.getInstance(mContext).removeGeoFence(MGEOFenceRequestId,removeListener);
OnAddGeofencesResultListener addListener=新建OnAddGeofencesResultListener(){
@凌驾
onAddGeofencesResult(int状态码,字符串[]GeoFenceRequestId)上的公共无效{
if(statusCode!=位置statusCode.SUCCESS){
//处理错误案例
}
其他的
Log.i(标记“已成功添加Geofence”+GeoFenceRequestId[0]+“用于监视”);
}
};
LocationHelper.getInstance(mContext.addGeoFence(list,PendingEvent,addListener);
}
下面是清单文件中的代码段
<service
android:name="com.myexample.sample.GeoFenceIntentService"
android:label="@string/app_name"
android:exported="true">
</service>
。
你检查过你得到的位置估计圈了吗?您可以使用模拟位置应用程序设置位置以及精度圆。您的geofence可能太小,无法容纳您的位置圈,这就是事件未被触发的原因。Android geofence从未启用GPS(因为其API非常糟糕,其设备功耗已经无法控制)。如果您希望地理围栏优于GPS,则必须设置地理围栏,然后不断单独轮询GPS
GPS轮询的处理程序可以为空,轮询的存在只是为了将准确信息强制输入其糟糕的位置API,从而触发围栏。您是否尝试使用
广播接收器作为挂起内容的目标,而不是服务
?如果没有,请尝试一下,看看是否调用了onReceive()
。