Ghi lại dữ liệu về hoạt động thể dục

API bản ghi 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 đăng ký thuê bao. Gói thuê bao được liên kết với ứng dụng Android và chứa loại dữ liệu về hoạt động thể dục hoặc 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 đã ghi có trong nhật ký thể dục của người dùng. Nếu bạn cũng muốn hiển thị 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 Bản ghi. Để ghi lại dữ liệu về sức khỏe với siêu dữ liệu phiên, hãy sử dụng API phiên.

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

Để yêu cầu thu thập dữ liệu cảm biến trong nền trong ứng dụng của bạn, hãy sử dụng phương thức RecordingClient.subscribe, như 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, Google Fit sẽ thay mặt ứng dụng lưu trữ dữ liệu về hoạt động thể dục TYPE_STEP_COUNT_DELTA trong nhật ký tập thể dục. Gói thuê bao này sẽ xuất hiện trong danh sách gói thuê bao đang hoạt động của ứng dụng.

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

Liệt kê gói thuê bao đang hoạt động

Để nhận danh sách các gói thuê bao đang hoạt động cho ứng dụng, hãy sử 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ý dữ liệu về hoạt động thể dục

Để dừng việc 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ư trong đoạn mã sau đây:

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.
        });
}