Próbuję użyć api geofencing z usług Google Play, ale nie mogę zagwarantować, że mój odbiornik wyemituje metodę onReceive()
.Interfejs API funkcji API geofencingowania BroadcastReceiver nie został uruchomiony
Wygląda na to, że szanuję wszystko, co jest potrzebne, aby działało, ale nie dostaję żadnego zdarzenia przejściowego.
- I obejmują usługi Google Play w moich zależności
- zażądać zgody
ACCESS_FINE_LOCATION
- I zarejestrować
BroadcastReceiver
z eksportowanego wartością ustawioną na „true” (niektórzy mówią, że jest to konieczne) w moim Oczywisty - Dodaję dużą strefę geofence w miejscu, w którym się znajduję (potwierdziłem to za pomocą Google Maps na moim telefonie komórkowym) z obydwoma typami przejść i bez wygaśnięcia.
- Próbowałem z dokładną pozorowaną lokalizacją w środku strefy geofence, i drugi z geofe nce 10 sekund później (i zrobiłem czy
ACCESS_MOCK_LOCATION
persmission był obecny) - I sprawdzeniu, że lokalizacja mojego telefon był on i jego tryb został ustawiony na dużą dokładnością
- I zweryfikowane połączenia internetowego
- próbowałem i bez odłączeniem LocationClient po dodaniu Geofence
Ale nadal, mam udane onAddGeofencesResult()
zwrotnego, ale nie onReceive()
zwrotnego. Próbowałem go z Nexusem 5 i Nexusem 7, a oba nie uruchamiały przejść w strefach geofence.
Próbowałem również próbki na https://developer.android.com/training/location/geofencing.html, ale jego zachowanie było identyczne.
Oto moja BroadcastReceiver
klasa:
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);
}
}
}
w moim Oczywista, mam następujące wiersze (nie w tej struktury)
<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" />
A w moim pliku build.gradle, ja obejmować usługi takie jak ta
dependencies {
compile 'com.google.android.gms:play-services:4.3.23'
}
Spędziłem cały dzień próbując dowiedzieć się, dlaczego tak się dzieje ". t praca ... Od Logcat znalazłem te linie, ale nie jestem pewien, czy są one związane z problemem, ponieważ nie znajduję posta w Google na temat tych błędów w 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#]
Jeśli nikt nie może mi pomóc, boję się, że jadę do wdrożenia mój własny algorytm Geofencing z LocationManager zarejestrowanej w GPS_PROVIDER, a to będzie drenażu baterii moich użytkowników ...
czy emulujesz lokalizacje GPS? Powiedziałbym, że precyzja geofence nie jest niezawodna –
Próbowałem też z pozorowanymi lokalizacjami.Użyłem lokalizacji pośrodku geofence z małą dokładnością. Zrobiło się dokładnie tak samo, gdy użyłem mojej prawdziwej lokalizacji. –
wydarzenie zostanie wystrzelone dopiero po wyjściu z geofence –