Ghi lại dữ liệu thể dục

Ghi lại API cho phép ứng dụng của bạn yêu cầu lưu trữ tự động dữ liệu cảm biến theo cách tiết kiệm pin bằng cách tạo các gói thuê bao. Gói thuê bao được liên kết với ứng dụng Android và bao gồm loại dữ liệu về hoạt động thể dục hoặc một nguồn dữ liệu cụ thể.

Bạn có thể tạo nhiều gói thuê bao cho các loại dữ liệu hoặc nguồn dữ liệu khác nhau trong ứng dụng của mình. Google Fit lưu trữ dữ liệu về hoạt động thể dục từ các gói thuê bao đang hoạt động, ngay cả khi ứng dụng của bạn không chạy và khôi phục các gói thuê bao này khi hệ thống khởi động lại.

Dữ liệu được ghi lại có trong lịch sử tập thể dục của người dùng. Nếu bạn cũng muốn hiện dữ liệu trong ứng dụng theo thời gian thực, hãy sử dụng API Cảm biến cùng với API ghi. Để ghi lại dữ liệu thể chất bằng siêu dữ liệu phiên, hãy sử dụng Sessions API (API Phiên).

Đăng ký nhận dữ liệu về hoạt động thể dục

Để yêu cầu thu thập dữ liệu cảm biến ở chế độ nền trong ứng dụng của bạn, hãy sử dụng phương thức RecordingClient.subscribe như minh hoạ trong đoạn mã sau:

Kotlin

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    // This example shows subscribing to a DataType, across all possible data
    // sources. Alternatively, a specific DataSource can be used.
    .subscribe(DataType.TYPE_STEP_COUNT_DELTA)
    .addOnSuccessListener {
        Log.i(TAG, "Successfully subscribed!")
    }
    .addOnFailureListener { e ->
        Log.w(TAG, "There was a problem subscribing.", e)
    }

Java

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        // This example shows subscribing to a DataType, across all possible
        // data sources. Alternatively, a specific DataSource can be used.
        .subscribe(DataType.TYPE_STEP_COUNT_DELTA)
        .addOnSuccessListener(unused ->
                Log.i(TAG, "Successfully subscribed!"))
        .addOnFailureListener( e ->
        Log.w(TAG, "There was a problem subscribing.", e));

Nếu bạn thêm gói thuê bao thành công, thì Google Fit sẽ thay mặt ứng dụng của bạn lưu trữ dữ liệu về hoạt động thể dục thuộc loại TYPE_STEP_COUNT_DELTA trong nhật ký hoạt động thể dục. Gói thuê bao này sẽ xuất hiện trong danh sách các gói thuê bao đang hoạt động cho ứng dụng của bạn.

Để đăng ký nhiều loại dữ liệu về hoạt động thể dục hơn trong ứng dụng, hãy làm theo các bước trong ví dụ trước nhưng mỗi lần cung cấp một loại dữ liệu về hoạt động thể dục khác nhau.

Liệt kê các gói đăng ký đang hoạt động

Để xem danh sách các gói thuê bao đang hoạt động cho ứng dụng của bạn, hãy dùng phương thức RecordingClient.listSubscriptions như trong đoạn mã sau:

Kotlin

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    .listSubscriptions()
    .addOnSuccessListener { subscriptions ->
        for (sc in subscriptions) {
            val dt = sc.dataType
            Log.i(TAG, "Active subscription for data type: ${dt.name}")
        }
    }

Java

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        .listSubscriptions()
        .addOnSuccessListener(subscriptions -> {
            for (Subscription sc : subscriptions) {
                DataType dt = sc.getDataType();
                Log.i(TAG, "Active subscription for data type: ${dt.name}");
            }
    });
}

Huỷ đăng ký nhận dữ liệu về hoạt động thể dục

Để dừng thu thập dữ liệu cảm biến trong ứng dụng, hãy sử dụng phương thức RecordingClient.unsubscribe như minh hoạ trong đoạn mã sau:

Kotlin

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    // This example shows unsubscribing from a DataType. A DataSource should
    // be used where the subscription was to a DataSource. Alternatively, if
    // the client doesn't maintain its subscription information, they can use
    // an element from the return value of listSubscriptions(), which is of
    // type Subscription.
    .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA)
    .addOnSuccessListener {
        Log.i(TAG,"Successfully unsubscribed.")
    }
    .addOnFailureListener { e->
        Log.w(TAG, "Failed to unsubscribe.")
        // Retry the unsubscribe request.
    }

Java

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        // This example shows unsubscribing from a DataType. A DataSource
        // should be used where the subscription was to a DataSource.
        // Alternatively, if the client doesn’t maintain its subscription
        // information, they can use an element from the return value of
        // listSubscriptions(), which is of type Subscription.
        .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA)
        .addOnSuccessListener(unused ->
            Log.i(TAG,"Successfully unsubscribed."))
        .addOnFailureListener(e -> {
            Log.w(TAG, "Failed to unsubscribe.");
            // Retry the unsubscribe request.
        });
}