Android 地理围栏 PendingIntent 未触发

Android Geofencing PendingIntent not firing

我有一个 GeofenceImplementorClass。我在其中添加了三个地理围栏,其中之一是我从 LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 获得的当前位置。我期待 IntentClass 服务得到执行,因为我已经在地理围栏位置之一,但是该服务没有得到执行。请帮忙。

public class GeofenceImplementor implements ResultCallback,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener{
    private ArrayList<Geofence> geofenceList = new ArrayList<>();
    private PendingIntent pendingIntent = null;
    private GoogleApiClient googleApiClient;
    private Boolean isConnected = false;
    private Boolean request = false;
    private Context context;

    public GeofenceImplementor(Context context){
        this.context = context;
        googleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        googleApiClient.connect();
    }

    public void addToList(LatLng latLng,String key){
        geofenceList.add(new Geofence.Builder()
                .setRequestId(key)
                .setCircularRegion(latLng.latitude, latLng.longitude, 1000)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .build());
    }

    private GeofencingRequest getGeofencingRequest() {
        GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
        builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
        builder.addGeofences(geofenceList);
        return builder.build();
    }

    private PendingIntent getPendingIntent(){
        if(pendingIntent != null)
            return  pendingIntent;

        Intent intent = new Intent(context,IntentClass.class);
        return PendingIntent.getService(context,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
    }

    public void implement() {
        if(isConnected) {
            try {
                LocationServices.GeofencingApi.addGeofences(googleApiClient,
                        getGeofencingRequest(), getPendingIntent()).setResultCallback(this);
            } catch (SecurityException ex) {
                Log.e("Security Exception", ex.getStackTrace().toString());
            }
        }
        else request = true;
    }
}

我当前的位置半径大于地理围栏。 将地理围栏圆半径增加到 3000 米解决了这个问题。