1

Je construis une application mobile avec laquelle je vais pouvoir tester le fonctionnement du géofencing sur les téléphones Android. J'ai une application qui utilise GoogleApiClient pour FusedLocationApi.requestLocationUpdates. Je reçois toutes les mises à jour que je montre sur la carte dans mon MapFragment. J'essaie ensuite d'ajouter les geofences avec GeofencingClient.Android Geofencing ne déclenche pas IntentService

private void addGeofenceList() { 
    mGeofencingClient.addGeofences(getGeofencingRequest(), getGeofencePendingIntent()) 
      .addOnSuccessListener(this, new OnSuccessListener<Void>() { 
       @Override 
       public void onSuccess(Void aVoid) { 
        Log.wtf(TAG, "addGeofences() - succesfully added"); 
       } 
      }) 
      .addOnFailureListener(this, new OnFailureListener() { 
       @Override 
       public void onFailure(@NonNull Exception e) { 
        Log.wtf(TAG, "addGeofences() - failed to add"); 
       } 
      }); 
} 

Ce code est exécuté et je reviens qu'ils ont été ajoutés avec succès. Mais quand je suis à l'endroit où la geofence devrait se déclencher, ne pas appeler le service IntentService. J'ajoute aussi le IntentService:

private PendingIntent getGeofencePendingIntent() { 
    if (mGeofencePendingIntent != null) { 
     return mGeofencePendingIntent; 
    } 
    Intent intent = new Intent(this, GeofenceService.class); 
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

Je crée aussi un GeofencingRequest où je mets le déclencheur initial lors de la saisie.

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

Et c'est là que je crée le ArrayList<Geofence> mGeofenceList où ajouter tous les geofences:

public void createGeofenceList() { 
    GeofenceList geofences = new GeofenceList(); 
    ArrayList<GeofenceModel> geofenceList = (ArrayList) geofences.returnGeofences(); 
    for (GeofenceModel geofence : geofenceList) { 
     mGeofenceList.add(new Geofence.Builder() 
       .setRequestId(geofence.getREQ_ID()) 
       .setCircularRegion(
         geofence.getLocation().latitude, 
         geofence.getLocation().longitude, 
         geofence.getRadius() 
       ) 
       .setExpirationDuration(geofence.getExpire()) 
       .setTransitionTypes(geofence.getTransition()) 
       .build()); 
    } 
    addGeofenceList(); 
} 

Mon ensemble des projets est sur GitHub. Toute l'aide sera appréciée!

Répondre

0

Dans votre manifeste remplacer votre déclaration de service qui est

<service android:name="services.GeofenceService" />

à cette

<service android:name="services.GeofenceService" android:exported="true"/>

Fondamentalement, vous devez marquer votre service comme exported si Android OS peut invoquer votre IntentService