11

Sto tentando di utilizzare l'API di geofencing dai servizi di riproduzione di Google, ma non riesco a far sì che il ricevitore di broadcast attivi il suo metodo onReceive().API di geofencing Android BroadcastReceiver non attivato

Sembra che rispetti tutto ciò che è necessario per farlo funzionare ma non ottengo alcun evento di transizione.

  • I includono i servizi di Google Play nelle mie dipendenze
  • Chiedo il ACCESS_FINE_LOCATION permesso
  • possibile registrare il BroadcastReceiver con il valore esportato impostato su "true" (alcuni dicono che è necessaria) nel mio Manifesto
  • aggiungo un grande geofence presso il luogo esatto in cui sono (ho convalidato con Google Maps sul mio cellulare) con entrambi i tipi di transizione e nessuna scadenza
  • ho provato con una posizione finto preciso nel mezzo del geofence, e un altro fuori dal geofe nce 10 secondi più tardi (e ho fatto in modo che il persmission ACCESS_MOCK_LOCATION era presente)
  • ho verificato che la posizione del mio cellulare era acceso e la sua modalità è stata impostata a elevata precisione
  • ho verificato la connessione a internet
  • ho provato con e senza scollegare il LocationClient dopo l'aggiunta del geofence

ma ancora, ho un successo onAddGeofencesResult() richiamata, ma non onReceive() callback. Ho provato con un Nexus 5 e un Nexus 7 ed entrambi non riescono a innescare le transizioni geofence.

Ho anche provato il campione su https://developer.android.com/training/location/geofencing.html, ma il suo comportamento era identico.

Ecco il mio BroadcastReceiver classe:

public class GeofencesReceiver extends BroadcastReceiver implements ConnectionCallbacks, OnConnectionFailedListener, OnAddGeofencesResultListener { 
    private final static String TAG = "GeofencesReceiver"; 

    private Context context = null; 
    private LocationClient locationClient = null; 

    /** 
    * An empty constructor is needed by the manifest. 
    */ 
    @SuppressWarnings("unused") 
    public GeofencesReceiver(){} 

    public GeofencesReceiver(Context context){ 
     this.context = context; 
     locationClient = new LocationClient(context, this, this); 
     locationClient.connect(); 
    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       List<Geofence> geofences = getGeofences(); 
       if(geofences.size() > 0){ 
        Intent geofenceBroadcastReceiverIntent = new Intent(context, GeofencesReceiver.class); 
        PendingIntent pi = PendingIntent.getBroadcast(context, 0, geofenceBroadcastReceiverIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
        locationClient.addGeofences(geofences, pi, GeofencesReceiver.this); 
       }else 
        Log.w(TAG, "No GPS zone found"); 
      } 
     }).start(); 
    } 

    private List<Geofence> getGeofences(){ 
     Vector<Geofence> geofences = new Vector<Geofence>(); 
     Geofence geofence = new Geofence.Builder() 
        .setRequestId("1") 
        .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) 
        .setCircularRegion(45.2676018, -72.1572185, 100) 
        .setExpirationDuration(Geofence.NEVER_EXPIRE) 
        .build(); 
     geofences.add(geofence); 
     return geofences; 
    } 

    @Override 
    public void onDisconnected() { 
     Log.d(TAG, "onDisconnected"); 
    } 

    @Override 
    public void onAddGeofencesResult(int i, String[] strings) { 
     if(i != LocationStatusCodes.SUCCESS) 
      Log.e(TAG, "Failed to add geofences"); 
     else 
      Log.d(TAG, "Geofences successfully added"); 
     locationClient.disconnect(); 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 
     Log.e(TAG, "onConnectionFailed"); 
    } 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "onReceive"); 
     if(LocationClient.hasError(intent)){ 
      int errorCode = LocationClient.getErrorCode(intent); 
      Log.e(TAG, "Location Services error: " + Integer.toString(errorCode)); 
      return; 
     } 
     int transitionType = LocationClient.getGeofenceTransition(intent); 
     if(transitionType == Geofence.GEOFENCE_TRANSITION_ENTER || transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) { 
      List<Geofence> triggerList = LocationClient.getTriggeringGeofences(intent); 
      for(Geofence geofence : triggerList){ 
       Log.d(TAG, (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER ? "Entering" : "Exiting") + " GPS zone " + geofence.getRequestId()); 
      } 
     }else{ 
      Log.e(TAG, "Geofence transition error: " + transitionType); 
     } 
    } 
} 

Nel mio manifesto, ho le seguenti righe (non in quella struttura)

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<receiver android:name="novom.anyware.anywaresdk.GeofencesReceiver" android:exported="true" /> 
<meta-data android:name="com.google.android.gms.version" android:value="4323000" /> 

E nel mio file build.gradle, ho includere il servizi come questo

dependencies { 
    compile 'com.google.android.gms:play-services:4.3.23' 
} 

ho trascorso l'intera giornata cercando di capire il motivo per cui esso doesn' t lavoro ... Da Logcat ho trovato queste righe, ma non sono sicuro che siano correlate al problema, dal momento che non trovo post su Google su tali errori con Geofencing.

04-07 09:01:14.631 1160-1319/? I/GCoreUlr﹕ Successfully inserted location 
04-07 09:01:14.631 1160-1319/? I/GCoreUlr﹕ Not calling LocationReportingService, hasMoved: true, elapsed millis: 580166, request: Phone 

04-07 09:02:16.481 1160-1319/? I/GCoreUlr﹕ Successfully inserted location 
04-07 09:02:16.501 1160-1319/? I/GCoreUlr﹕ Starting service, intent=Intent { cmp=com.google.android.gms/com.google.android.location.reporting.LocationReportingService }, extras=null 
04-07 09:02:16.571 1210-1252/? W/GLSUser﹕ GoogleAccountDataService.getToken() 
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Failed write_ctrl(u 69) res=-1 errno=22 
04-07 09:02:16.601 1160-3780/? I/qtaguid﹕ Untagging socket 69 failed errno=-22 
04-07 09:02:16.601 1160-3780/? W/NetworkManagementSocketTagger﹕ untagSocket(69) failed with errno -22 
04-07 09:02:16.601 1160-3780/? I/imp﹕ I/O exception (org.apache.http.NoHttpResponseException) caught when processing request: The target server failed to respond 
04-07 09:02:16.601 1160-3780/? I/imp﹕ Retrying request 
04-07 09:02:17.501 1160-6156/? I/GCoreUlr﹕ Starting service, intent=Intent { act=com.google.android.location.reporting.ACTION_UPDATE_ACTIVE_STATE cmp=com.google.android.gms/com.google.android.location.reporting.service.DispatchingService }, extras=null 
04-07 09:02:17.511 1160-6156/? I/GCoreUlr﹕ Batch Location Update succeeded for account account#7# 
04-07 09:02:17.571 1160-1319/? I/GCoreUlr﹕ Ensuring that reporting is active for [account#7#] 

Se nessuno mi può aiutare, ho paura ho intenzione di implementare il mio algoritmo Geofencing con la LocationManager registrato con il GPS_PROVIDER, e questo sta per esaurire la batteria dei miei utenti ...

+0

stai emulando le posizioni GPS? direi che la precisione del geofence non è affidabile –

+0

Ho provato anche con posizioni simulate.Ho usato una posizione nel mezzo del Geofence con una piccola precisione. È stato esattamente come quando ho usato la mia posizione reale. –

+0

l'evento scatterà solo quando esci dal Geofence –

risposta

9

Prima di tutto, ti sei assicurato che le tue coordinate siano quelle giuste? Ho avuto un problema molto simile mentre giocavo con il geofencing e mi ci è voluto un po 'per capire che le mie coordinate erano semplicemente invertite.

Inutile dire che mi sono sentito stupido.

+1

Sì, mi sono appena reso conto che il ragazzo che testava le mie cose aveva inserito le coordinate invertite. Dovrei sicuramente aggiungere più log nel mio codice ... –

Problemi correlati