Bắt đầu

Tài liệu này giải thích cách bắt đầu phát triển bằng API Nhận biết trên Android. API Nhận biết là một phần của Dịch vụ Google Play.

Để sử dụng API nhận thức, bạn cần có Tài khoản Google. Nếu đã có tài khoản thì bạn có thể bỏ qua bước này. Bạn cũng nên có một Tài khoản Google riêng cho mục đích thử nghiệm.

Trước khi bắt đầu

Lấy khoá API

Nếu bạn chưa bật API nhận thức và chưa có khoá API của Google, hãy làm theo các bước trong phần Đăng ký và khoá API.

Định cấu hình ứng dụng

  1. Trong tệp build.gradle cấp dự án, hãy thêm kho lưu trữ Maven của Google vào cả hai mục buildscriptallprojects:

    buildscript {
        repositories {
            google()
        }
    }
    
    allprojects {
        repositories {
            google()
        }
    }
    
  2. Thêm các phần phụ thuộc cho API Nhận thức vào tệp Gradle cấp ứng dụng của mô-đun, thường là app/build.gradle:

    dependencies {
      implementation 'com.google.android.gms:play-services-awareness:19.1.0'
    }
    
  3. Thêm Khoá API nhận biết vào tệp AndroidManifest.xml của ứng dụng. Để làm như vậy, hãy thêm thẻ <meta-data> với android:name="com.google.android.awareness.API_KEY". Đối với android:value, hãy chèn Khoá API nhận biết của riêng bạn, được đặt trong dấu ngoặc kép.

    <manifest>
        <application>
            <meta-data
                android:name="com.google.android.awareness.API_KEY"
                android:value="API_KEY"/>
        </application>
    </manifest>
  4. Thêm các quyền cần thiết vào tệp AndroidManifest.xml của ứng dụng. Các quyền bắt buộc sẽ khác nhau, tuỳ thuộc vào phương thức API và loại hàng rào mà ứng dụng của bạn sử dụng.

Lệnh gọi mẫu

Lệnh gọi mẫu sau đây đến getDetectedActivity() minh hoạ cách sử dụng mô hình Dịch vụ Google Play không có kết nối với API Nhận biết:

    // 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");
            }
        })

Các bước tiếp theo

Tìm hiểu thêm về các API khác nhau trong API Nhận thức: