Get started

This document explains how to start to develop with the Awareness API on Android. The Awareness API is part of Google Play services.

To use the Awareness API, you need a Google Account. If you already have an account, then you're all set. You might also want a separate Google Account for testing purposes.

Before you begin

Obtain an API key

If you haven't already enabled the Awareness API and obtained a Google API key, follow the steps in Signup and API keys to do so.

Configure your app

  1. In your project-level build.gradle file, include Google's Maven repository in both your buildscript and allprojects sections:

    buildscript {
        repositories {
            google()
        }
    }
    
    allprojects {
        repositories {
            google()
        }
    }
    
  2. Add the dependencies for the Awareness API to your module's app-level Gradle file, which is usually app/build.gradle:

    dependencies {
      implementation 'com.google.android.gms:play-services-awareness:19.1.0'
    }
    
  3. Add your Awareness API Key to your app's AndroidManifest.xml file. To do so, add a <meta-data> tag with android:name="com.google.android.awareness.API_KEY". For android:value, insert your own Awareness API Key, surrounded by quotation marks.

    <manifest>
        <application>
            <meta-data
                android:name="com.google.android.awareness.API_KEY"
                android:value="API_KEY"/>
        </application>
    </manifest>
    
  4. Add the necessary permissions to your app's AndroidManifest.xml file. The required permissions vary, depending on the API methods and fence types that your app uses.

Example call

The following example call to getDetectedActivity() demonstrates how to use the connectionless Google Play services model with the Awareness API:

    // Each type of contextual information in the snapshot API has a corresponding "get" method.
    // For instance, this is how to get the user's current Activity.
    Awareness.getSnapshotClient(this).getDetectedActivity()
        .addOnSuccessListener(new OnSuccessListener<DetectedActivityResponse>() {
            @Override
            public void onSuccess(DetectedActivityResponse dar) {
                ActivityRecognitionResult arr = dar.getActivityRecognitionResult();
                // getMostProbableActivity() is good enough for basic Activity detection.
                // To work within a threshold of confidence,
                // use ActivityRecognitionResult.getProbableActivities() to get a list of
                // potential current activities, and check the confidence of each one.
                DetectedActivity probableActivity = arr.getMostProbableActivity();

                int confidence = probableActivity.getConfidence();
                String activityStr = probableActivity.toString();
                mLogFragment.getLogView().println("Activity: " + activityStr
                    + ", Confidence: " + confidence + "/100");
            }
        })

Next steps

Learn more about the different APIs within the Awareness API: