Courtesy: GitHub

Android Geofencing Sample

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.

Introduction

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.Builder. Each Geofence object contains the following information:

  1. Latitude, longitude, and radius
  2. Expiration time
  3. Transition type
  4. Geofence ID

Read more about geofences in Creating and Monitoring Geofences.

Pre-requisites

  • Android SDK 26
  • Android Build Tools v26.0.1
  • Android Support Repository

Screenshots

Screenshot

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

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.

License

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

http://www.apache.org/licenses/LICENSE-2.0

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.

compile "com.android.support:support-v4:26.1.0"

compile "com.android.support:support-v13:26.1.0"

compile "com.android.support:cardview-v7:26.1.0"

compile "com.android.support:appcompat-v7:26.1.0"

compile 'com.google.android.gms:play-services-location:11.4.0'

compile 'com.google.android.gms:play-services-wearable:11.4.0'

compile 'com.android.support:support-v13:26.1.0'

compileSdkVersion 26

minSdkVersion 18

targetSdkVersion 22

compile 'com.google.android.gms:play-services-wearable:11.4.0'

compile 'com.android.support:support-v13:26.1.0'

compile 'com.google.android.support:wearable:2.0.5'

compileSdkVersion 26

versionCode 1

versionName "1.0"

minSdkVersion 18

targetSdkVersion 22

package com.example.android.wearable.geofencing

uses-permission

  • android.permission.ACCESS_FINE_LOCATION

package com.example.android.wearable.geofencing

ConfirmationActivity

ConfirmationActivity

HomeListenerService

CheckInAndDeleteDataItemsService

HomeListenerService

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.

CheckInAndDeleteDataItemsService

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.