Why your android application needs awareness api

Google introduced android awareness api in I/O 16. It offers a lot of awesome features which can make your android application better and improve the user experience. Awareness API is a combination of two APIs

Fence API

This API is an enhancement over geofencing. It can call a callback when user enters or leaves a particular location. It also applies on things like headphones, your app can receive a callback when headphones are connected or disconnect. We will see and example of geofencing in a while.

Snapshot API

Using snapshot API you can get the information about user’s current context. You can access user activity, nearby beacons, weather condition in that area etc.

How awareness API can boost your application

Awareness api is very powerful as it can give the entire context of the user. Using that context you can make your application smart and act according to the user conditions. We will see an example where we will advertise newly introduced and less known feature of our app with the help of awareness api.

Advertise your new indoor map feature using Fence API

Lets say you are working on an airline application. You introduced the new indoor airport map feature in your application. You want your travelers to use this new feature when they are within an airport.

Now lets take a hypothetical scenario, there is a traveller Bob who is traveling with your airline for the first time. He is new to town and has never been to the airport. He just reached the airport and want to go to gate A2 for boarding. Can your application help Bob to reach to gate A2?

The answer is yes, because you know with the help of awareness API that Bob is on the airport. That is good enough input for your application to prompt Bob with a notification mentioning your new feature of indoor airport maps.

Getting to know the location of user and prompting them with notification sounds good, but how to achieve this or in other words how to code this? Don’t worry its not as complicated as it sounds. Lets see the code.

Location Aware App

To make this article more helpful, I have created a project on github which uses awareness API to prompt user with notification when user reaches a configured location.

How to use Awareness API

To start using awareness API you need to create an API key from google console. Once you create the key add it in your AndroidManifest.xml file as shown below.

Note: The key which I have used above is a fake key, it will not work. To make it work you have to generate your own key and replace the above key with your newly created key.

Now that our setup is done, lets start using awareness API in our app. Lets take the above example of advertising your new indoor map feature of your app. For this what you would need is list of airports locations where you have enabled indoor map.

Once you have latitude and longitude of all the location we can start registering those locations in awareness API, so that whenever user reaches nearby those location your app gets a callback. Lets take a look at the code for this.

Here we are creating GoogleApiClient and adding the Awareness and LocationServices API to it. Now lets take a look at GeofenceApiHelper and what it does in registerLocations() method.

To register locations we have to connect to the google client first and pass it a callback saying whenever you are connected, register these locations. Now lets see what regiserGeoFence() does.

In this method we are registering the locations and setting the trigger INITIAL_TRIGGER_ENTER, which means whenever user enters this location, your app will get a callback. All this is done using GeofencingRequest. This request takes our locations and geofenceRequestRegistrar registers this request. Now how are we getting the locationsToBeRegistered(). Lets take a look.

Locations are nothing but a List of Geofences. Here we are creating two Geofence, one for Atlanta airport and another for Chicago airport.

Ok, now we have the GeofencingRequest  ready, now we need to register this using GeofenceRegistrar. Lets take a look at the registration code.

Registering geofencingRequest is a simple task, you just have to use LocationServices.GeofencingApi to add the request. But what is this LocationAwareService? Ok, so what happens when user comes nearby the registered location? Your awareness api need something e.g callback which it can trigger.

LocationAwareService is an IntentService which acts like a callback and is triggered by the awareness API. Lets take a look at the LocationAwareService code.

Whenever awareness api detects that user is nearby the configured location, it will call the onHandleIntent() method of this service. Then we can extract the location information from the intent. In this case I am just sending a broadcast to show a notification in the phone. But for your usecase you can do whatever you want when the user is at your configured location.

Thats all folks! I hope this will help you to make your application better.

 

Leave a Reply

Your email address will not be published. Required fields are marked *