-1

У меня есть GeofenceImplementorClass. Я добавляю в него три геозоны, один из которых - мое текущее местоположение, которое я получил от . Я ожидаю, что служба IntentClass будет выполнена, так как я уже нахожусь в одном из мест геопозиции, но служба не выполняется. Пожалуйста помоги.Android Geofencing PendingIntent не работает

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; 
    } 
} 

ответ

1

Мой текущий радиус местоположения был больше, чем геозонность. Увеличение радиуса окружности Geofence до 3000 м решило проблему.

+0

Что вы подразумеваете под «текущим радиусом местоположения»? –

+0

Радиус круга, который отображается вокруг вашего текущего местоположения на вашем устройстве. – user1232138