4

我试图实施地理围栏机制,其中监控地理围栏,一旦用户退出当前地理围栏,当前坐标用于创建新的地理围栏并启动数据库查询以获取一些数据。未解决的意图未解雇

我的问题是,挂起的意图永远不会被解雇。

从日志中我可以看到geofences被添加到位置客户端。然而,在位置改变时没有待处理的意图(我将栅栏半径设置为2米,并且走过了100米)。我宣布意向服务的方式有问题吗?

这是intent服务类。

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

您是否试过使用'BroadcastReceiver'作为您的'PendingIntent'而不是'Service'的目标?如果不是,请尝试一下,看看是否调用了“onReceive()”。 –

回答

0

Read this片段。 你有没有检查过你的位置估计圈?您可以使用模拟位置应用来设置位置以及准确性圈。您的地理围栏可能太小,无法容纳您的位置圈,因此这些事件不会被触发。

0

Android GeoFences永远不会启用GPS(因为它们的API非常糟糕,而且它们的设备功耗已经如此失控)。您必须设置您的地理围栏,然后如果要通过GPS进行地理围栏,请不断分别调查GPS。

GPS轮询的处理程序可以为空,轮询只存在强制准确的信息到他们糟糕的位置API并反过来触发围栏。