2013-06-11 16 views
5

Zacząłem pracować nad systemem Android z ostatnią funkcją usług lokalizacyjnych: Geofences !! Czy jest jakiś znany problem z fałszywym dostawcą lokalizacji? Poniższy przykład tutaj (https://developer.android.com/training/location/geofencing.html) moja usługa intent nigdy nie została uruchomiona, nawet jeśli obecna lokalizacja znajduje się w strefie geofence. Używam aplikacji Android FakeGPS jako fałszywego dostawcy lokalizacji i jeśli zasymuluję trasę, widzę zmiany lokalizacji w aplikacji Mapy Google, więc fałszywy dostawca lokalizacji działa dobrze. Jakieś pomysły ?Geofence na Androida z pozornym dostawcą lokalizacji

Dzięki. Paolo.

Odpowiedz

3

Próbowałem na zawsze, aby to zadziałało. Co za ból Google! Ponieważ mówi, geofences można łatwo przetestować za pomocą mocks.

Magiczną sztuczką jest użycie nazwy dostawcy "sieć" w lokalizacji przekazanej do setMockLocation.

Location location = new Location("network"); 
    location.setLatitude(latitude); 
    location.setLongitude(longitude); 
    location.setTime(new Date().getTime()); 
    location.setAccuracy(3.0f); 
    location.setElapsedRealtimeNanos(System.nanoTime()); 

    LocationServices.FusedLocationApi.setMockLocation(_googleApiClient, location); 
+2

Don musisz najpierw wywołać LocationServices.FusedLocationApi.setMockMode (_googleApiClient, true)? –

+0

Czy masz łącze, które pokazuje, że LocationServices.GeofencingApi faktycznie używa LocationServices.FusedLocationApi do kpiny? Nie widziałem żadnych dowodów na to, że jest to prawda. – yiati

0

Pamiętaj, aby włączyć fałszywe lokalizacje w telefonie. Wybierz Ustawienia-> Opcje programisty -> "Zezwalaj na pozorowane lokalizacje".

0

LocationServices.FusedLocationApi.setMockMode (googleApiClient, true) musi być stosowany przed ustawieniem Mock gości.

1

Właściwie usługa Intent użyta we wspomnianym przykładzie działa dobrze, jeśli aplikacja jest na pierwszym planie, ale gdy aplikacja działa w tle, ta usługa IntentService nigdy nie jest wywoływana. Tak więc musimy użyć usługi Broadcast-Receiver zamiast usługi Intent.

Ten blog okazał się pomocny w uzyskaniu rozwiązania.

http://davehiren.blogspot.in/2015/01/android-geofence-stop-getting.html

+0

Link do rozwiązania jest mile widziany, ale upewnij się, że twoja odpowiedź jest przydatna bez niego: [dodaj kontekst związany z linkiem] (// meta.stackexchange.com/a/8259), aby inni użytkownicy mieli pojęcie, co to jest i dlaczego tam jest, a następnie zacytuj najważniejszą część strony, do której prowadzi link, jeśli strona docelowa jest niedostępna. [Odpowiedzi, które są niewiele więcej niż linkem, mogą zostać usunięte.] (// stackoverflow.com/help/deleted-answers) –

+0

Podczas gdy ten link może odpowiedzieć na pytanie, lepiej jest dołączyć istotne części odpowiedzi tutaj i dostarczyć link do odniesienia. Odpowiedzi dotyczące linków mogą stać się nieprawidłowe, jeśli strona z linkami się zmieni. - [Z recenzji] (/ opinia/niskiej jakości-posty/15725231) – Serenity

+0

Nie powinieneś zgłaszać odpowiedzi, dopóki nie uzyskasz jasnego zrozumienia pytania i jego rozwiązania. @Serenity –

1

Można używać odbiornika audycji zamiast aktywności jak ten

public class GeofenceReceiver extends BroadcastReceiver 
implements 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    ResultCallback<Status>{ 

GoogleApiClient mGoogleApiClient; 
PendingIntent mGeofencePendingIntent ; 
Context mContext; 

@Override 
public void onReceive(Context context, Intent intent) { 
    mContext = context; 
    mGoogleApiClient = new GoogleApiClient.Builder(mContext) 
      .addOnConnectionFailedListener(this) 
      .addConnectionCallbacks(this) 
      .addApi(LocationServices.API) 
      .build(); 

    mGoogleApiClient.connect(); 
} 



@Override 
public void onConnected(@Nullable Bundle bundle) { 
    try { 
     LocationServices.GeofencingApi.addGeofences(
       mGoogleApiClient, 
       // The GeofenceRequest object. 
       getGeofencingRequest(), 
       getGeofencePendingIntent() 
     ).setResultCallback(this); // Result processed in onResult(). 
    } catch (SecurityException securityException) { 
     Log.i(getClass().getSimpleName(),securityException.getMessage()); 
    } 
} 

// Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission. 
@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

/** 
* Runs when the result of calling addGeofences() and removeGeofences() becomes available. 
* Either method can complete successfully or with an error. 
* 
* Since this activity implements the {@link ResultCallback} interface, we are required to 
* define this method. 
* 
* @param status The Status returned through a PendingIntent when addGeofences() or 
*    removeGeofences() get called. 
*/ 
@Override 
public void onResult(@NonNull Status status) { 
    if (status.isSuccess()) { 
     Log.i(getClass().getSimpleName(),"Success"); 
    } else { 
     // Get the status code for the error and log it using a user-friendly message. 
     Log.i(getClass().getSimpleName(),getErrorString(status.getStatusCode())); 
    } 
} 

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

private List<Geofence> getGeofecne(){ 
    List<Geofence> mGeofenceList = new ArrayList<>(); 

    //add one object 
    mGeofenceList.add(new Geofence.Builder() 
      // Set the request ID of the geofence. This is a string to identify this 
      // geofence. 
      .setRequestId("key") 

      // Set the circular region of this geofence. 
      .setCircularRegion(
        25.768466, //lat 
        47.567625, //long 
        50) // radios 

      // Set the expiration duration of the geofence. This geofence gets automatically 
      // removed after this period of time. 
      //1000 millis * 60 sec * 5 min 
      .setExpirationDuration(1000 * 60 * 5) 

      // Set the transition types of interest. Alerts are only generated for these 
      // transition. We track entry and exit transitions in this sample. 
      .setTransitionTypes(
        Geofence.GEOFENCE_TRANSITION_DWELL) 
      //it's must to set time in millis with dwell transition 
      .setLoiteringDelay(3000) 
      // Create the geofence. 
      .build()); 

    return mGeofenceList; 

} 

private PendingIntent getGeofencePendingIntent() { 
    // Reuse the PendingIntent if we already have it. 
    if (mGeofencePendingIntent != null) { 
     return mGeofencePendingIntent; 
    } 
    Intent intent = new Intent(mContext, GeofenceTransitionsIntentService.class); 
    return PendingIntent.getService(mContext, 0, intent, PendingIntent. 
      FLAG_UPDATE_CURRENT); 
} 

}

check out my repo, jest pełny przykład wykorzystania Geofence https://github.com/3zcs/Geofence

Powiązane problemy