Java Code Examples for com.google.android.gms.location.GeofencingEvent#getTriggeringGeofences()
The following examples show how to use
com.google.android.gms.location.GeofencingEvent#getTriggeringGeofences() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UtilityService.java From wear-os-samples with Apache License 2.0 | 6 votes |
/** * Called when a geofence is triggered */ private void geofenceTriggered(Intent intent) { Log.v(TAG, ACTION_GEOFENCE_TRIGGERED); // Check if geofences are enabled boolean geofenceEnabled = Utils.getGeofenceEnabled(this); // Extract the geofences from the intent GeofencingEvent event = GeofencingEvent.fromIntent(intent); List<Geofence> geofences = event.getTriggeringGeofences(); if (geofenceEnabled && geofences != null && geofences.size() > 0) { if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER) { // Trigger the notification based on the first geofence showNotification(geofences.get(0).getRequestId(), Constants.USE_MICRO_APP); } else if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) { // Clear notifications clearNotificationInternal(); clearRemoteNotifications(); } } UtilityReceiver.completeWakefulIntent(intent); }
Example 2
Source File: GeofenceBroadcastReceiver.java From Wrox-ProfessionalAndroid-4E with Apache License 2.0 | 6 votes |
@Override public void onReceive(Context context, Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { int errorCode = geofencingEvent.getErrorCode(); String errorMessage = GeofenceStatusCodes.getStatusCodeString(errorCode); Log.e(TAG, errorMessage); } else { // Get the transition type. int geofenceTransition = geofencingEvent.getGeofenceTransition(); // A single event can trigger multiple geofences. // Get the geofences that were triggered. List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); // TODO React to the Geofence(s) transition(s). } }
Example 3
Source File: ReceiveTransitionsIntentService.java From Leanplum-Android-SDK with Apache License 2.0 | 6 votes |
@Override protected void onHandleIntent(Intent intent) { try { GeofencingEvent event = GeofencingEvent.fromIntent(intent); if (event.hasError()) { int errorCode = event.getErrorCode(); Log.d("Location Client Error with code: " + errorCode); } else { int transitionType = event.getGeofenceTransition(); List<Geofence> triggeredGeofences = event.getTriggeringGeofences(); if (transitionType == Geofence.GEOFENCE_TRANSITION_ENTER || transitionType == Geofence.GEOFENCE_TRANSITION_EXIT) { LocationManagerImplementation locationManager = (LocationManagerImplementation) ActionManager.getLocationManager(); if (locationManager != null) { locationManager.updateStatusForGeofences(triggeredGeofences, transitionType); } } } } catch (Throwable t) { Util.handleException(t); } }
Example 4
Source File: UtilityService.java From io2015-codelabs with Apache License 2.0 | 6 votes |
/** * Called when a geofence is triggered */ private void geofenceTriggered(Intent intent) { Log.v(TAG, ACTION_GEOFENCE_TRIGGERED); // Check if geofences are enabled boolean geofenceEnabled = Utils.getGeofenceEnabled(this); // Extract the geofences from the intent GeofencingEvent event = GeofencingEvent.fromIntent(intent); List<Geofence> geofences = event.getTriggeringGeofences(); if (geofenceEnabled && geofences != null && geofences.size() > 0) { if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER) { // Trigger the notification based on the first geofence showNotification(geofences.get(0).getRequestId(), Constants.USE_MICRO_APP); } else if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) { // Clear notifications clearNotificationInternal(); clearRemoteNotifications(); } } UtilityReceiver.completeWakefulIntent(intent); }
Example 5
Source File: UtilityService.java From io2015-codelabs with Apache License 2.0 | 6 votes |
/** * Called when a geofence is triggered */ private void geofenceTriggered(Intent intent) { Log.v(TAG, ACTION_GEOFENCE_TRIGGERED); // Check if geofences are enabled boolean geofenceEnabled = Utils.getGeofenceEnabled(this); // Extract the geofences from the intent GeofencingEvent event = GeofencingEvent.fromIntent(intent); List<Geofence> geofences = event.getTriggeringGeofences(); if (geofenceEnabled && geofences != null && geofences.size() > 0) { if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_ENTER) { // Trigger the notification based on the first geofence showNotification(geofences.get(0).getRequestId(), Constants.USE_MICRO_APP); } else if (event.getGeofenceTransition() == Geofence.GEOFENCE_TRANSITION_EXIT) { // Clear notifications clearNotificationInternal(); clearRemoteNotifications(); } } UtilityReceiver.completeWakefulIntent(intent); }
Example 6
Source File: GeofenceHandler.java From CodenameOne with GNU General Public License v2.0 | 6 votes |
@Override protected void onHandleIntent(Intent intent) { String className = intent.getStringExtra("geofenceClass"); String id = intent.getStringExtra("geofenceID"); GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); for (com.google.android.gms.location.Geofence gf : geofencingEvent.getTriggeringGeofences()) { try { id = gf.getRequestId(); GeofenceListener l = (GeofenceListener) Class.forName(className).newInstance(); if (geofencingEvent.getGeofenceTransition() == com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_ENTER) { l.onEntered(id); } else if (geofencingEvent.getGeofenceTransition() == com.google.android.gms.location.Geofence.GEOFENCE_TRANSITION_EXIT) { l.onExit(id); } } catch (Exception e) { Log.e("Codename One", "geofence error", e); } } }
Example 7
Source File: GeofenceData.java From android-sdk with MIT License | 6 votes |
protected static List<GeofenceData> from(GeofencingEvent event) { String problem = check(event); if (problem != null) { throw new IllegalArgumentException(problem); } ArrayList<GeofenceData> result = new ArrayList<>(); for (Geofence geofence : event.getTriggeringGeofences()) { try { result.add(new GeofenceData(geofence.getRequestId())); } catch (IllegalArgumentException ex) { Logger.log.logError(ex.getMessage()); } } if (result.isEmpty()) { throw new IllegalArgumentException("GeofencingEvent has no triggering geofences"); } return result; }
Example 8
Source File: GeofenceData.java From android-sdk with MIT License | 6 votes |
private static String check(GeofencingEvent event) { if (event == null) { return "GeofencingEvent is null"; } if (event.hasError()) { return "GeofencingEvent has error: "+event.getErrorCode(); } if (event.getTriggeringGeofences() == null) { return "GeofencingEvent has no triggering geofences"; } if (event.getTriggeringGeofences().isEmpty()) { return "GeofencingEvent has empty list of triggering geofences"; } int transition = event.getGeofenceTransition(); //Only GEOFENCE_TRANSITION_ENTER and GEOFENCE_TRANSITION_EXIT are supported for now. if (transition != Geofence.GEOFENCE_TRANSITION_ENTER && transition != Geofence.GEOFENCE_TRANSITION_EXIT) { return "GeofencingEvent has invalid transition: "+transition; } return null; }
Example 9
Source File: GeoTransitionHelper.java From mobile-messaging-sdk-android with Apache License 2.0 | 5 votes |
/** * Resolves transition information from geofencing intent * * @param intent geofencing intent * @return transition information * @throws RuntimeException if information cannot be resolved */ static GeoTransition resolveTransitionFromIntent(Intent intent) throws RuntimeException { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent == null) { throw new RuntimeException("Geofencing event is null, cannot process"); } if (geofencingEvent.hasError()) { if (geofencingEvent.getErrorCode() == GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE) { throw new GeofenceNotAvailableException(); } throw new RuntimeException("ERROR: " + GeofenceStatusCodes.getStatusCodeString(geofencingEvent.getErrorCode())); } GeoEventType event = supportedTransitionEvents.get(geofencingEvent.getGeofenceTransition()); if (event == null) { throw new RuntimeException("Transition is not supported: " + geofencingEvent.getGeofenceTransition()); } Set<String> triggeringRequestIds = new ArraySet<>(); for (Geofence geofence : geofencingEvent.getTriggeringGeofences()) { triggeringRequestIds.add(geofence.getRequestId()); } Location location = geofencingEvent.getTriggeringLocation(); return new GeoTransition(event, triggeringRequestIds, new GeoLatLng(location.getLatitude(), location.getLongitude())); }
Example 10
Source File: GeofenceIntentService.java From PowerSwitch_Android with GNU General Public License v3.0 | 5 votes |
/** * Handles incoming intents. * * @param intent sent by Location Services. This Intent is provided to Location * Services (inside a PendingIntent) when addGeofences() is called. */ @Override protected void onHandleIntent(Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { Log.e(this, "GeofencingError"); return; } // Get the transition type. int geofenceTransition = geofencingEvent.getGeofenceTransition(); // Test that the reported transition was of interest. if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { // Get the geofences that were triggered. A single event can trigger // multiple geofences. List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); // Get the Ids of each geofence that was triggered. ArrayList<String> triggeringGeofencesIdsList = new ArrayList<>(); for (Geofence geofence : triggeringGeofences) { triggeringGeofencesIdsList.add(geofence.getRequestId()); } Log.d(this, getTransitionString(geofenceTransition) + ": " + TextUtils.join(", ", triggeringGeofencesIdsList)); executeGeofences(triggeringGeofences, geofenceTransition); } else { // Log the error. Log.e(this, "Unknown Geofence transition: " + geofenceTransition); } }
Example 11
Source File: GeofenceTransitionsIntentService.java From io2015-codelabs with Apache License 2.0 | 5 votes |
@Override protected void onHandleIntent(Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { String errorMessage = GeofenceErrorMessages.getErrorString(this, geofencingEvent.getErrorCode()); Log.e(TAG, errorMessage); return; } // Get the transition type. int geofenceTransition = geofencingEvent.getGeofenceTransition(); // Test that the reported transition was of interest. if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { // Get the geofences that were triggered. A single event can trigger multiple geofences. List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); // Get the transition details as a String. String geofenceTransitionDetails = getGeofenceTransitionDetails( this, geofenceTransition, triggeringGeofences ); // Send notification and log the transition details. sendNotification(geofenceTransitionDetails); Log.i(TAG, geofenceTransitionDetails); } else { // Log the error. } }
Example 12
Source File: GeofenceTransitionsJobIntentService.java From location-samples with Apache License 2.0 | 5 votes |
/** * Handles incoming intents. * @param intent sent by Location Services. This Intent is provided to Location * Services (inside a PendingIntent) when addGeofences() is called. */ @Override protected void onHandleWork(Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { String errorMessage = GeofenceErrorMessages.getErrorString(this, geofencingEvent.getErrorCode()); Log.e(TAG, errorMessage); return; } // Get the transition type. int geofenceTransition = geofencingEvent.getGeofenceTransition(); // Test that the reported transition was of interest. if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { // Get the geofences that were triggered. A single event can trigger multiple geofences. List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); // Get the transition details as a String. String geofenceTransitionDetails = getGeofenceTransitionDetails(geofenceTransition, triggeringGeofences); // Send notification and log the transition details. sendNotification(geofenceTransitionDetails); Log.i(TAG, geofenceTransitionDetails); } else { // Log the error. Log.e(TAG, getString(R.string.geofence_transition_invalid_type, geofenceTransition)); } }