Courtesy: GitHub

Creating and Monitoring Geofences

Demonstrates how to create and remove geofences using the GeofencingApi. Monitor geofence transitions and creates notifications whenever a device enters or exits a geofence.

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.

Location Services treats a geofences as an area rather than as a points and proximity. This allows it to detect when the user enters or exits a geofence. For each geofence, you can ask Location Services to send you entrance events, exit events, or both. You can also limit the duration of a geofence by specifying an expiration duration in milliseconds. After the geofence expires, Location Services automatically removes it.

To run this sample, location must be enabled.

Prerequisites

  • Android API Level >v9
  • Android Build Tools >v21
  • Google Support Repository

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-play-location/issues

Patches are encouraged, and may be submitted according to the instructions in CONTRIBUTING.md.

License

Copyright 2014 Google, 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.

compileSdkVersion 25

applicationId "com.google.android.gms.location.sample.geofencing"

minSdkVersion 16

targetSdkVersion 25

versionCode 1

versionName "1.0"

compile 'com.android.support:appcompat-v7:25.3.1'

testCompile 'junit:junit:4.12'

compile 'com.android.support:design:25.3.1'

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

compile 'com.google.android.gms:play-services-maps:11.0.0'

package com.google.android.gms.location.sample.geofencing

uses-permission

  • android.permission.ACCESS_FINE_LOCATION

MainActivity

Demonstrates how to create and remove geofences using the GeofencingApi. Uses an IntentService to monitor geofence transitions and creates notifications whenever a device enters or exits a geofence.

This sample requires a device's Location settings to be turned on. It also requires the ACCESS_FINE_LOCATION permission, as specified in AndroidManifest.xml.

Tracks whether the user requested to add or remove geofences, or to do neither.

Provides access to the Geofencing API.

The list of geofences used in this sample.

Used when requesting to add or remove geofences.

Builds and returns a GeofencingRequest. Specifies the list of geofences to be monitored. Also specifies how the geofence notifications are initially triggered.

Adds geofences, which sets alerts to be notified when the device enters or exits one of the specified geofences. Handles the success or failure results returned by addGeofences().

Adds geofences. This method should be called after the user has granted the location permission.

Removes geofences, which stops further notifications when the device enters or exits previously registered geofences.

Removes geofences. This method should be called after the user has granted the location permission.

Runs when the result of calling {@link #addGeofences()} and/or {@link #removeGeofences()} is available. @param task the resulting Task, containing either a result or error.

Gets a PendingIntent to send with the request to add or remove Geofences. Location Services issues the Intent inside this PendingIntent whenever a geofence transition occurs for the current list of geofences. * @return A PendingIntent for the IntentService that handles geofence transitions.

This sample hard codes geofence data. A real app might dynamically create geofences based on the user's location.

Ensures that only one button is enabled at any time. The Add Geofences button is enabled if the user hasn't yet added geofences. The Remove Geofences button is enabled if the user has added geofences.

Shows a {@link Snackbar} using {@code text}. * @param text The Snackbar text.

Shows a {@link Snackbar}. * @param mainTextStringId The id for the string resource for the Snackbar text. @param actionStringId The text of the action item. @param listener The listener associated with the Snackbar action.

Returns true if geofences were added, otherwise false.

Stores whether geofences were added ore removed in {@link SharedPreferences}; * @param added Whether geofences were added or removed.

Performs the geofencing task that was pending until location permission was granted.

Return the current state of the permissions needed.

Callback received when a permissions request has been completed.

GeofenceTransitionsIntentService

Listener for geofence transition changes. * Receives geofence transition events from Location Services in the form of an Intent containing the transition type and geofence id(s) that triggered the transition. Creates a notification as the output.

This constructor is required, and calls the super IntentService(String) constructor with the name for a worker thread.

Handles incoming intents. @param intent sent by Location Services. This Intent is provided to Location Services (inside a PendingIntent) when addGeofences() is called.

Gets transition details and returns them as a formatted string. * @param geofenceTransition The ID of the geofence transition. @param triggeringGeofences The geofence(s) triggered. @return The transition details formatted as String.

Posts a notification in the notification bar when a transition is detected. If the user clicks the notification, control goes to the MainActivity.

Maps geofence transition types to their human-readable equivalents. * @param transitionType A transition type constant defined in Geofence @return A String indicating the type of transition