このドキュメントでは、Android で Awareness API を使用した開発を開始する方法について説明します。Awareness API は Google Play 開発者サービスの一部です。
Awareness API を使用するには、Google アカウントが必要です。すでにアカウントをお持ちの場合は、準備が整っています。テスト用に別の Google アカウントを使用することもできます。
始める前に
API キーを取得する
Awareness API をまだ有効にしておらず、Google API キーを取得していない場合は、登録と API キーの手順に沿って有効化と取得を行ってください。
アプリを設定する
プロジェクト レベルの
build.gradleファイルで、buildscriptセクションとallprojectsセクションの両方に Google の Maven リポジトリを含めます。buildscript { repositories { google() } } allprojects { repositories { google() } }Awareness API の依存関係をモジュールのアプリレベルの Gradle ファイル(通常は
app/build.gradle)に追加します。dependencies { implementation 'com.google.android.gms:play-services-awareness:19.1.0' }Awareness API キーをアプリの
AndroidManifest.xmlファイルに追加します。追加するには、android:name="com.google.android.awareness.API_KEY"を持つ<meta-data>タグを追加します。android:valueには、ご自身の Awareness API キーを引用符で囲んで挿入します。<manifest> <application> <meta-data android:name="com.google.android.awareness.API_KEY" android:value="API_KEY"/> </application> </manifest>
アプリの
AndroidManifest.xmlファイルに必要な権限を追加します。必要な権限は、アプリが使用する API メソッドとフェンスの種類によって異なります。
通話の例
次の getDetectedActivity() の呼び出し例は、Awareness API で接続なしの Google Play サービス モデルを使用する方法を示しています。
    // 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");
            }
        })
次のステップ
Awareness API 内のさまざまな API について詳しくは、以下をご覧ください。