When the user enters the vicinity of the Android building (B44) or the Yerba Buena Gardens near the Moscone center in San Francisco, a notification silently appears on their wearable with an option to check in. This notification automatically disappears when they leave the area, and reappears the next time they are at one of these locations.
Geofencing combines awareness of the user's current location with awareness of nearby features, defined as the user's proximity to locations that may be of interest. To mark a location of interest, you specify its latitude and longitude. To adjust the proximity for the location, you add a radius. The latitude, longitude, and radius define a geofence. You can have multiple active geofences at one time.
To use geofencing, start by defining the geofences you want to monitor.
Although you usually store geofence data in a local database or download
it from the network, you need to send a geofence to Location Services as
an instance of Geofence, which you create with
Geofence object contains the following information:
Read more about geofences in Creating and Monitoring Geofences.
This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.
If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-Geofencing
Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.
Copyright 2017 The Android Open Source Project, Inc.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Listens to DataItem events on the wearable device.
Listen for DataItems added/deleted from the geofence service running on the companion.
Deletes the check-in notification when the DataItem is deleted. @param dataItem Used only for logging in this sample, but could be used to identify which notification to cancel (in this case, there is at most 1 notification).
Posts a local notification for the given geofence id, with an option to check in. @param geofenceId The geofence id that the user has triggered. @param dataItemUri The Uri for the DataItem that triggered this notification. Used to delete this DataItem when the notification is dismissed.
Handles "Check In" action on the location-based notification. Also deletes orphan DataItems when a notification is dismissed from the wearable.
Helper method to create confirmation animations on the wearable. @param animationType Defined by constants in ConfirmationActivity. @param message The message to display with the animation.