Remember, the Snapshot API is used to request current state while the Fence API continuously checks for a specified state and sends callbacks when an app isn't running.
Overall, there are a few basic steps in order to use the Snapshot API or Fence API:
Get an API key from the Google Developers Console
Add necessary permissions and API key to the manifest:
<!-- Not required for getting current headphone state -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Only required for actvity recognition -->
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/>
<!-- Replace with your actual API key from console -->
<meta-data android:name="com.google.android.awareness.API_KEY"
android:value="YOUR_API_KEY"/>
<!-- Required for Snapshot API only -->
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
Initalize the GoogleApiClient
somewhere, preferably in your activity's onCreate() method.
GoogleApiClient client = new GoogleApiClient.Builder(context)
.addApi(Awareness.API)
.build();
client.connect();
Call the API of your choice
Parse result
An easy way to check for the needed user permission is a method such as this:
private boolean isFineLocationGranted() {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Log.e(getClass().getSimpleName(), "Fine location permission not granted!");
}
}