I have been doing serious research on this topic for many days ... I also saw a lot of topics ... But, unfortunately, I could not find a solution ....
I am writing an application that uses the new Google API for Geofence ... Well, I can handle the "ins" and "outs" from geofence, but only if my application is open! Even if I connect to Wi-Fi, gps and 3G, but the application does not fire any event ... Just if the application is open ...
I use the exact same GeofenceRequester class of the documentation http://developer.android.com/training/location/geofencing.html .
Even the class was the same, I will post the code here:
package br.com.marrs.imhere.geofence; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.content.IntentSender.SendIntentException; import android.os.Bundle; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import br.com.marrs.imhere.services.ReceiveTransitionsIntentService; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.GooglePlayServicesClient; import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener; import com.google.android.gms.location.Geofence; import com.google.android.gms.location.LocationClient; import com.google.android.gms.location.LocationClient.OnAddGeofencesResultListener; import com.google.android.gms.location.LocationStatusCodes; public class GeofenceRequester implements OnAddGeofencesResultListener, ConnectionCallbacks, OnConnectionFailedListener {
And the service:
package br.com.marrs.imhere.services; import br.com.marrs.imhere.ImHereActivity; import br.com.marrs.imhere.R; import com.google.android.gms.location.Geofence; import com.google.android.gms.location.LocationClient; import android.app.IntentService; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; import java.util.List; public class ReceiveTransitionsIntentService extends IntentService { public ReceiveTransitionsIntentService() { super("ReceiveTransitionsIntentService"); } @Override protected void onHandleIntent(Intent intent) {
And the Broadcast receiver in Office:
public class GeofenceSampleReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {
And here is the code snippet that I use to create GEofence before submitting it to GeofenceRequester.
int raio = Integer.parseInt(spinner.getAdapter().getItem(spinner.getSelectedItemPosition()).toString()); int transitionType = (in.isChecked())?Geofence.GEOFENCE_TRANSITION_ENTER:Geofence.GEOFENCE_TRANSITION_EXIT; Geofence geofence = new Geofence.Builder().setRequestId(nomeGeofence.getText().toString()).setTransitionTypes(transitionType).setCircularRegion(lat, lon, raio).setExpirationDuration(Geofence.NEVER_EXPIRE).build(); geofences.add(geofence); try { mGeofenceRequester.addGeofences(geofences); addCircleGeofence(raio); } catch (UnsupportedOperationException e) { Toast.makeText(getActivity(), "Já existe uma requisição de add em andamento",Toast.LENGTH_LONG).show(); }
Any help would be great! Thanks!