5

Mein Geofence arbeitet am Start, aber dann plötzlich nach einem Tag oder zwei Stops auszulösen, gibt es ein Problem auf der Google-Seite hier oder meinen Code?Geofence funktioniert aber nach einer Weile nicht mehr Triggerung

Während des Bootvorgangs und Starten der App verwende ich ein IntentService, die dann einen Geofence-Register:

public class RegisterGeoIntentService extends IntentService implements 
GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> { 

private static final String TAG = "RegisterGeoIS"; 

private static final long TIME_OUT = 100; 
protected GoogleApiClient mGoogleApiClient; 
protected ArrayList<Geofence> mGeofenceList; 
private PendingIntent mGeofencePendingIntent; 

public RegisterGeoIntentService() { 
    super(TAG); 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.i(TAG, "Creating Register Geo Intent service"); 
    mGeofenceList = new ArrayList<Geofence>(); 
    mGeofencePendingIntent = null; 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    buildGoogleApiClient(); 
    populateGeofenceList(); 
    mGoogleApiClient.blockingConnect(TIME_OUT, TimeUnit.MILLISECONDS); 
    String connected = mGoogleApiClient.isConnected() ? "connected" : "disconnected"; 
    Log.i(TAG, "Restoring geofence - status: " + connected); 

    String mode = null; 
    if(intent != null) { 
     mode = intent.getStringExtra(GEOFENCE_MODE); 
     if(mode.equals(GEOFENCE_MODE_START)) { 
      removeGeofencesButtonHandler(); 
      addGeofencesButtonHandler(); 
     } else { 
      removeGeofencesButtonHandler(); 
     } 
    } else { 
     Log.e(TAG, "No Intent data, could not start Geo"); 
    } 
} 

@Override 
public void onConnected(Bundle bundle) { 
    Log.i(TAG, "Connected to GoogleApiClient"); 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.i(TAG, "Connection suspended"); 
} 

@Override 
public void onConnectionFailed(ConnectionResult result) { 
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); 
} 

@Override 
public void onResult(Status status) {  // Not used, using await 
    if(status.isSuccess()) { 
     Log.i(TAG, "Geofences added"); 
     mGoogleApiClient.disconnect(); 
    } else { 
     Log.i(TAG, "Geofences not successful"); 
     String errorMessage = GeoErrorMessages.getErrorString(this, status.getStatusCode()); 
     Log.e(TAG, errorMessage); 
    } 
} 

public void addGeofencesButtonHandler() { 
    if(!mGoogleApiClient.isConnected()) { 
     Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    Status result = null; 
    try { 
     result = LocationServices.GeofencingApi.addGeofences(
      mGoogleApiClient, 
      getGeofencingRequest(), 
      getGeofencePendingIntent() 
     ).await(TIME_OUT, TimeUnit.MILLISECONDS); 
    } catch (SecurityException securityException) { // TODO Catch if manually disabled 
     // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission. 
     logSecurityException(securityException); 
    } 
    if(result != null) { 
     Log.i(TAG, "Trying to add Geofences - result: " + result.toString()); 
    } else { 
     Log.i(TAG, "Trying to add Geofences - result: is null"); 
    } 
} 

public void removeGeofencesButtonHandler() { 
    if(!mGoogleApiClient.isConnected()) { 
     Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    Status result = null; 
    try { 
     result = LocationServices.GeofencingApi.removeGeofences(mGoogleApiClient, getGeofencePendingIntent() 
     ).await(TIME_OUT, TimeUnit.MILLISECONDS); 
    } catch (SecurityException securityException) { // TODO Catch if manually disabled 
     // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission. 
     logSecurityException(securityException); 
    } 
    if(result != null) { 
     Log.i(TAG, "Trying to remove Geofences - result: " + result.toString()); 
    } else { 
     Log.i(TAG, "Trying to remove Geofences - result: is null"); 
    } 
} 

private GeofencingRequest getGeofencingRequest() { 
    GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); 
    // The INITIAL_TRIGGER_ENTER flag indicates that geofencing service should trigger a 
    // GEOFENCE_TRANSITION_ENTER notification when the geofence is added and if the device 
    // is already inside that geofence. 
    builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); 
    builder.addGeofences(mGeofenceList); 
    return builder.build(); 
} 

private PendingIntent getGeofencePendingIntent() { 
    if(mGeofencePendingIntent != null) { 
     return mGeofencePendingIntent; 
    } 
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling 
    // addGeofences() and removeGeofences(). 
    // Removed, using Broadcast now 
    // Intent intent = new Intent(this, GeoTransitionsIntentService.class); 
    // return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    Intent intent = new Intent("com.xyz.app.ACTION_RECEIVE_GEOFENCE"); 
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

private void logSecurityException(SecurityException securityException) { 
    Log.e(TAG, "Invalid location permission. You need to use ACCESS_FINE_LOCATION with geofences", securityException); 
} 

public void populateGeofenceList() { 
    RealmHelper realmHelper = RealmHelper.getInstance(this); 
    for(Map.Entry<String, LatLng> entry : realmHelper.queryLandMarks().entrySet()) { 
     mGeofenceList.add(new Geofence.Builder() 
     .setRequestId(entry.getKey()) 
     .setCircularRegion(
      entry.getValue().latitude, 
      entry.getValue().longitude, 
      Constants.GEOFENCE_RADIUS_IN_METERS) 
     .setExpirationDuration(Geofence.NEVER_EXPIRE) 
     .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT) 
     .build()); 
    } 
} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .addApi(LocationServices.API) 
     .build(); 
} 
} 

Und:

public class GeofenceReceiver extends BroadcastReceiver { 
... 

@Override 
public void onReceive(Context context, Intent intent) { 
this.context = context; 

GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); 
if(geofencingEvent.hasError()) { 
    String errorMessage = GeoErrorMessages.getErrorString(context, geofencingEvent.getErrorCode()); 
    Log.e(TAG, errorMessage); 
    return; 
} 

int geofenceTransition = geofencingEvent.getGeofenceTransition(); 

if(geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { 

// Do stuff 
} 
... 

auch meine Antwort sehen, was ich versuchte, und dachte, dass ich festgelegt. Aber es hat nicht geholfen.

+0

* Ich habe versucht, DozeMode für diese App zu deaktivieren * das ist nicht möglich –

+0

Unter Android, Setti ngs, Battery, Battery Optimization, Alle Apps ... wählen und nicht optimieren in Nexus 6P – powder366

+0

Das tut nicht, was Sie denken, dass es tut. Sie können den Schlummermodus für eine App nicht deaktivieren –

Antwort

0

Bitte beachten Sie, dies nicht wirklich

Mein GeoFence Hilfe wurde immer dann ausgelöst, aber die IntentService war das Problem

private PendingIntent getGeofencePendingIntent() { 
    if(mGeofencePendingIntent != null) { 
     return mGeofencePendingIntent; 
    } 
    Intent intent = new Intent(this, GeoTransitionsIntentService.class); 
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back  when calling 
    // addGeofences() and removeGeofences(). 
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 

geändert, um eine BroadcastReceiver in AndroidManifest als

definiert mit
private PendingIntent getGeofencePendingIntent() { 
    if(mGeofencePendingIntent != null) { 
     return mGeofencePendingIntent; 
    } 
    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling 
    // addGeofences() and removeGeofences(). 
    // Removed, using Broadcast now 
    // Intent intent = new Intent(this, GeoTransitionsIntentService.class); 
    // return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    Intent intent = new Intent("com.xyz.myapp.ACTION_RECEIVE_GEOFENCE"); 
    return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
} 
+0

Das hat eigentlich nicht funktioniert. – powder366

+0

Akzeptieren Sie nicht Ihre eigene Antwort, wenn sie nicht funktioniert. Warten Sie, bis Sie von anderen die richtigen Lösungen erhalten haben. – james

+0

Wie ich bereits erwähnt habe, dachte ich, dass ich es behoben habe, aber es war nicht die Lösung (und ich kann diese Antwort nicht löschen, nachdem ich gesagt habe, dass es Teil der Geschichte ist). – powder366

Verwandte Themen