待定意图未触发。

4
我正在尝试实现一个围栏机制,其中监视一个围栏,一旦用户退出当前围栏,就使用当前坐标创建新的围栏并启动数据库查询以获取某些数据。
我的问题是挂起意图从未被触发。
从日志中可以看到,围栏已添加到位置客户端中。然而,在位置更改时没有触发任何挂起的意图(我将围栏半径设置为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
2个回答

0

阅读此文。 你检查过你得到的位置估计圆了吗?你可以使用模拟位置应用程序来设置位置以及精度圆。你的地理围栏可能太小,无法容纳你的位置圆,这就是为什么事件没有被触发的原因。


0

Android GeoFences从不启用GPS(因为它们的API很糟糕,设备功耗已经如此之高)。如果您想要通过GPS进行地理围栏,您必须设置好您的地理围栏,然后单独不断轮询GPS。

GPS轮询的处理程序可以为空,该轮询仅存在于将准确信息强制输入其糟糕的位置API并触发围栏。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接