removed qué que geofencingclient geofences geofence español app added android google-maps geofencing

android - qué - Geofence no disparando



geofencingclient (2)

Geofence implementar Geofence en mi aplicación y el problema es que no se activa cuando estoy en el lugar que he marcado.

Lo que hago es configurar la Latitude y la Longitude de un selector de lugar .

Los valores que recibo están bien porque los puse en google maps y el lugar es el correcto, porque he leído muchas respuestas de que la gente estaba estableciendo valores incorrectos de lat / long .

Tengo dos clases: GeofenceSingleton.class y GeofenceTransitionsIntentService .

El GeofenceSingleton.class ve así:

public class GeofenceSingleton implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<Status> { private GoogleApiClient googleApiClient; private static GeofenceSingleton _singleInstance; private static Context appContext; private static final String ERR_MSG = "Application Context is not set!! " + "Please call GeofenceSngleton.init() with proper application context"; private PendingIntent mGeofencePendingIntent; private static ArrayList<Geofence> mGeofenceList; private GeofenceSingleton() { if (appContext == null) throw new IllegalStateException(ERR_MSG); this.googleApiClient = new GoogleApiClient.Builder(this.appContext) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build(); this.googleApiClient.connect(); mGeofenceList = new ArrayList<Geofence>(); } /** * @param applicationContext */ public static void init(Context applicationContext) { appContext = applicationContext; } public static GeofenceSingleton getInstance() { if (_singleInstance == null) synchronized (GeofenceSingleton.class) { if (_singleInstance == null) _singleInstance = new GeofenceSingleton(); } return _singleInstance; } @Override public void onConnected(Bundle bundle) { } @Override public void onConnectionSuspended(int i) { } @Override public void onConnectionFailed(ConnectionResult connectionResult) { } public void addGeofence(Location location, String uid) { mGeofenceList.add(new Geofence.Builder() .setRequestId(uid) .setCircularRegion( location.getLatitude(), location.getLongitude(), 500 ) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER) .setExpirationDuration(1000000) .build()); } private GeofencingRequest getGeofencingRequest() { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(mGeofenceList); return builder.build(); } private PendingIntent getGeofencePendingIntent() { // Reuse the PendingIntent if we already have it. if (mGeofencePendingIntent != null) { return mGeofencePendingIntent; } Intent intent = new Intent(appContext, GeofenceSingleton.class); // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling // addGeofences() and removeGeofences(). return PendingIntent.getService(appContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } public void startGeofencing() { if (!googleApiClient.isConnected()) { Toast.makeText(appContext, "API client not connected", Toast.LENGTH_SHORT).show(); return; } LocationServices.GeofencingApi.addGeofences( googleApiClient, // The GeofenceRequest object. getGeofencingRequest(), // A pending intent that that is reused when calling removeGeofences(). This // pending intent is used to generate an intent when a matched geofence // transition is observed. getGeofencePendingIntent() ).setResultCallback(this); // Result processed in onResult(). Toast.makeText(appContext,"Geofencing started", Toast.LENGTH_LONG).show(); } public void removeGeofence(){ LocationServices.GeofencingApi.removeGeofences( googleApiClient, // This is the same pending intent that was used in addGeofences(). getGeofencePendingIntent() ); } @Override public void onResult(Status status) { if (status.isSuccess()) { // Update state and save in shared preferences. Toast.makeText( appContext, "YO", Toast.LENGTH_SHORT ).show(); } else { Toast.makeText( appContext, "NOO", Toast.LENGTH_SHORT ).show(); } } }

Y GeofenceTransitionsIntentService

public class GeofenceTransitionsIntentService extends IntentService { private static final String TAG = "Geofence-Service"; private Handler handler; SharedPreferences sp; SharedPreferences.Editor editor; String EmailName, MessageEmail; Context mContext; Boolean EmailSent = false; public GeofenceTransitionsIntentService() { super(TAG); } @Override public void onCreate() { super.onCreate(); this.mContext = this; sp = PreferenceManager.getDefaultSharedPreferences(this); handler = new Handler(); } @Override protected void onHandleIntent(Intent intent) { final GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { Log.e(TAG, "Error in geofencing event"); return; } // Get the transition type. final int geofenceTransition = geofencingEvent.getGeofenceTransition(); // Test that the reported transition was of interest. if (geofenceTransition == 1) { handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Entered", Toast.LENGTH_SHORT).show(); sendNotification(); } }); Log.i(TAG, "Entered"); } else { // Log the error. handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "Sorry you are out", Toast.LENGTH_SHORT).show(); } }); Log.e(TAG, String.valueOf(geofenceTransition)); } } private void sendNotification() { Toast.makeText(GeofenceTransitionsIntentService.this, "HEEEEEEEY I''M IN", Toast.LENGTH_SHORT).show(); }

En mi MainActivity , tengo esto:

GeofenceSingleton.init(this); //If I don''t call this the class doesn''t work this.geofenceSingleton = GeofenceSingleton.getInstance();

Y luego construyo una Geofence siguiente manera:

Location geofence = new Location(""); geofence.setLatitude(Double.parseDouble(latitude)); geofence.setLongitude(Double.parseDouble(longitude)); geofenceSingleton.addGeofence(geofence, GeofenceKey); geofenceSingleton.startGeofencing();

NOTAS

He añadido <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> en mi manifest.xml

Decidí que el service era <service android:name=".GeofenceTransitionsIntentService"/>

Y agregué esta línea en mi build.gradle : compile ''com.google.android.gms:play-services:7.8.0''

Solo muestra Toast.makeText(appContext,"YO",Toast.LENGTH_SHORT).show(); eso es porque el estado es exitoso, y también muestra Toast.makeText(appContext,"Geofencing started", Toast.LENGTH_LONG).show(); significa que ha comenzado.

PREGUNTA

¿Por qué mi IntentService nunca se llama?

Lo probé poniendo un Lat / Long de mi casa y desde que estoy encendido debe ser activado, ¿no es así?


Como ya respondo aquí , Geofence API es algo así como un receptor de ubicación pasiva, es decir, debe solicitar coordenadas de LocationServices.FusedLocationApi.requestLocationUpdates en paralelo cuando necesita Geofence para funcionar.


Nunca funcionará porque lo que estás haciendo es

Intent intent = new Intent(appContext, GeofenceSingleton.class);

Pasando intento a GeofenceSingleton?

Lo que debes hacer es

Intent intent = new Intent(appContext, GeofenceTransitionsIntentService.class);