Roberto Orgiu


We already saw an overview of the new Awareness API from Google, but we didn’t dive into the Snapshot version of it. Instead of reacting to changes like the Fence API, these have been created with a polling method in mind, so that it’s us, the developers, requesting the updates whenever we need it.

Enabling the APIs


Together with the Awareness API (that we already enabled for the Fence API), we also need to add the Google Places API for Android if we want to recover the surroundings of our users. If, however, we don’t need to recover the nearby places, we can skip this part, as we would only need the Awareness API.

As for the Fence APIs, most of the Snapshots need to access the fine location, so we are well aware of the fact that we might need request runtime permissions.

General Overview

Within the SnapshotApi class, we get a static method for each of the contexts we can observe, and each of them will return a PendingResult typed with the kind of result you want to get. Being a PendingResult means that we can either await the result synchronously (or with a timeout) or set a callback, that will execute the code asynchronously and give us the result inside a listener.

Each type wrapped in the PendingResult extends Result itself, so that we have for each of the streams the same algorithm based on checking the success of the operation and then managing the result.

Using the APIs

We already saw that we can understand pretty easily what our user is doing, and the Snapshot API makes no exception: We can check what is the most probable activity our users are performing and also obtain a complete list of all the other possibilities with their accuracy:



In a very similar way, we can ask for the user’s location and obtain it quite easily:



Or we could just look what is nearby the device, obtaining a list of places from the Google Places API (which is why we need to enable it in the Developer Console):



And even get the current weather, with a full description of the temperature, condition and humidity:



And even get the current weather, with a full description of the temperature, the condition and the humidity:



These last three calls need the ACCESS_FINE_LOCATION permission.


As we can see, using the Snapshot API is infinitely easier than the older counterpart, where we were requested to write all the code to glue the different APIs, and all the calls have the same structure (as they all inherit from the same parent), so that we can implement everything with the glance of an eye. So… let’s go out there and make awesome context-aware apps!

Roberto_Orgiu.png Roberto Orgiu

 Roberto is a seasoned Android developer who has been coding on the platform since version 2.0. He is one of the organizers of DroidCon Italy and is a great voice in the Android community. You can find him writing about Android on his medium blog, when he is not coding for enNova.

Related Search Term(s): Tutorials

Create, Design, Develop and Connect at AnDevCon D.C. 2017!

Thoughts? Leave a comment: