このドキュメントでは、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' }
アプリの
AndroidManifest.xml
ファイルに Awareness API キーを追加します。追加するには、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 の詳細は次のとおりです。