我正在尝试实现一个围栏机制,其中监视一个围栏,一旦用户退出当前围栏,就使用当前坐标创建新的围栏并启动数据库查询以获取某些数据。
我的问题是挂起意图从未被触发。
从日志中可以看到,围栏已添加到位置客户端中。然而,在位置更改时没有触发任何挂起的意图(我将围栏半径设置为2m,我已经走了100米以上)。我声明意图服务的方式是否有问题?
以下是意图服务类。
这是来自清单文件的片段。
我的问题是挂起意图从未被触发。
从日志中可以看到,围栏已添加到位置客户端中。然而,在位置更改时没有触发任何挂起的意图(我将围栏半径设置为2m,我已经走了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>
BroadcastReceiver
作为你的PendingIntent
的目标,而不是一个Service
吗?如果还没有,请尝试一下并查看onReceive()
是否被调用。 - David Wasser