시작하기

이 문서에서는 Android에서 Awareness API를 사용하여 개발을 시작하는 방법을 설명합니다. Awareness API는 Google Play 서비스의 일부입니다.

Awareness API를 사용하려면 Google 계정이 필요합니다. 계정이 있는 경우 별도의 조치를 취할 필요가 없습니다. 테스트 목적으로 별도의 Google 계정이 필요할 수도 있습니다.

시작하기 전에

API 키 가져오기

아직 Awareness API를 사용 설정하고 Google API 키를 가져오지 않았다면 가입 및 API 키의 단계에 따라 사용 설정합니다.

앱 구성

  1. 프로젝트 수준의 build.gradle 파일에서 buildscriptallprojects 섹션에 Google의 Maven 저장소를 포함합니다.

    buildscript {
        repositories {
            google()
        }
    }
    
    allprojects {
        repositories {
            google()
        }
    }
    
  2. 모듈의 앱 수준 Gradle 파일(일반적으로 app/build.gradle)에 Awareness API의 종속 항목을 추가합니다.

    dependencies {
      implementation 'com.google.android.gms:play-services-awareness:19.1.0'
    }
    
  3. 앱의 AndroidManifest.xml 파일에 인식 API 키를 추가합니다. 이렇게 하려면 android:name="com.google.android.awareness.API_KEY"가 있는 <meta-data> 태그를 추가합니다. android:value의 경우 내 인지도 API 키를 따옴표로 묶어 삽입하세요.

    <manifest>
        <application>
            <meta-data
                android:name="com.google.android.awareness.API_KEY"
                android:value="API_KEY"/>
        </application>
    </manifest>
  4. 앱의 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에 대해 자세히 알아보세요.