Ghi lại dữ liệu thể dục
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
API 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 trong một
tiết kiệm pin bằng cách tạo gói thuê bao. Đã liên kết một gói thuê bao
bằng ứng dụng Android và chứa một loại dữ liệu hoạt động thể dục hoặc một dữ liệu cụ thể
nguồn.
Bạn có thể tạo nhiều gói thuê bao cho nhiều loại dữ liệu hoặc nguồn dữ liệu
trong ứng dụng của bạn. Google Fit lưu trữ dữ liệu 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 đồng thời 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ý tập 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 của bạn 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
dữ liệu hoạt động thể dục với siêu dữ liệu về phiên hoạt động, hãy sử dụng
API phiên.
Đăng ký dữ liệu 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
RecordingClient.subscribe
như minh hoạ trong đoạn mã sau đây:
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ẽ lưu trữ dữ liệu thể dục
dữ liệu thuộc loại
TYPE_STEP_COUNT_DELTA
trong lịch sử thể dục thay mặt cho ứng dụng của bạn. Gói thuê bao này 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 khá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 mỗi lần cung cấp một loại dữ liệu thể dục khác.
Liệt kê các gói thuê bao đ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 sử dụng
RecordingClient.listSubscriptions
như minh hoạ trong đoạn mã sau đây:
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}");
}
});
}
Hủy đăng ký dữ liệu thể dục
Để dừng thu thập dữ liệu cảm biến trong ứng dụng, hãy sử dụng
RecordingClient.unsubscribe
như minh hoạ 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.
});
}
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-31 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eThe Recording API enables your app to automatically store sensor data in a battery-efficient way through subscriptions, which are linked to your app and specify a data type or source.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Fit retains fitness data from active subscriptions even when your app is inactive, and these subscriptions are automatically restored upon system restart.\u003c/p\u003e\n"],["\u003cp\u003eYou can manage your active data subscriptions by listing existing ones and unsubscribing from specific data types as needed.\u003c/p\u003e\n"],["\u003cp\u003eTo display real-time data alongside recorded historical data in your app, consider using the Recording API in conjunction with the Sensors API.\u003c/p\u003e\n"]]],[],null,["# Record fitness data\n\nThe Recording API lets your app request automated storage of sensor data in a\nbattery-efficient manner by creating subscriptions. A subscription is associated\nwith an Android app and consists of a fitness data type or a specific data\nsource.\n\nYou can create multiple subscriptions for different data types or data sources\nin your app. Google Fit stores fitness data from active subscriptions,\neven when your app isn't running, and it restores these subscriptions when the\nsystem restarts.\n\nThe recorded data is available in the user's fitness history. If you also want\nto show the data in your app in real time, use the\n[Sensors API](/fit/android/sensors) together with the Recording API. To record\nfitness data with session metadata, use the\n[Sessions API](/fit/android/using-sessions).\n| **Note:** For best practices when you manage user data, see [Google Fit Developer and User Data Policy](/fit/policy).\n\nSubscribe to fitness data\n-------------------------\n\nTo request background collection of sensor data in your app, use the\n[`RecordingClient.subscribe`](/android/reference/com/google/android/gms/fitness/RecordingClient)\nmethod, as shown in the following code snippet: \n\n### Kotlin\n\n```kotlin\nFitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))\n // This example shows subscribing to a DataType, across all possible data\n // sources. Alternatively, a specific DataSource can be used.\n .subscribe(DataType.TYPE_STEP_COUNT_DELTA)\n .addOnSuccessListener {\n Log.i(TAG, \"Successfully subscribed!\")\n }\n .addOnFailureListener { e -\u003e\n Log.w(TAG, \"There was a problem subscribing.\", e)\n }\n```\n\n### Java\n\n```java\nFitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))\n // This example shows subscribing to a DataType, across all possible\n // data sources. Alternatively, a specific DataSource can be used.\n .subscribe(DataType.TYPE_STEP_COUNT_DELTA)\n .addOnSuccessListener(unused -\u003e\n Log.i(TAG, \"Successfully subscribed!\"))\n .addOnFailureListener( e -\u003e\n Log.w(TAG, \"There was a problem subscribing.\", e));\n```\n\nIf the subscription is added successfully, Google Fit stores fitness\ndata of type\n[`TYPE_STEP_COUNT_DELTA`](/android/reference/com/google/android/gms/fitness/data/DataType#TYPE_STEP_COUNT_DELTA)\nin the fitness history on behalf of your app. This subscription appears in the\nlist of active subscriptions for your app.\n\nTo subscribe to more types of fitness data in your app, follow the steps in the\nprevious example but provide a different fitness data type each time.\n| **Note:** Apps that run on Android 10 (API level 29) or higher must [get an Android permission](/fit/android/authorization#requesting_android_permissions) when they record some physical activity data types.\n\nList active subscriptions\n-------------------------\n\nTo get a list of the active subscriptions for your app, use the\n[`RecordingClient.listSubscriptions`](/android/reference/com/google/android/gms/fitness/RecordingClient)\nmethod, as shown in the following code snippet: \n\n### Kotlin\n\n```kotlin\nFitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))\n .listSubscriptions()\n .addOnSuccessListener { subscriptions -\u003e\n for (sc in subscriptions) {\n val dt = sc.dataType\n Log.i(TAG, \"Active subscription for data type: ${dt.name}\")\n }\n }\n```\n\n### Java\n\n```java\nFitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))\n .listSubscriptions()\n .addOnSuccessListener(subscriptions -\u003e {\n for (Subscription sc : subscriptions) {\n DataType dt = sc.getDataType();\n Log.i(TAG, \"Active subscription for data type: ${dt.name}\");\n }\n });\n}\n```\n\nUnsubscribe from fitness data\n-----------------------------\n\nTo stop the collection of sensor data in your app, use the\n[`RecordingClient.unsubscribe`](/android/reference/com/google/android/gms/fitness/RecordingClient)\nmethod, as shown in the following code snippet: \n\n### Kotlin\n\n```kotlin\nFitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))\n // This example shows unsubscribing from a DataType. A DataSource should\n // be used where the subscription was to a DataSource. Alternatively, if\n // the client doesn't maintain its subscription information, they can use\n // an element from the return value of listSubscriptions(), which is of\n // type Subscription.\n .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA)\n .addOnSuccessListener {\n Log.i(TAG,\"Successfully unsubscribed.\")\n }\n .addOnFailureListener { e-\u003e\n Log.w(TAG, \"Failed to unsubscribe.\")\n // Retry the unsubscribe request.\n }\n```\n\n### Java\n\n```java\nFitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))\n // This example shows unsubscribing from a DataType. A DataSource\n // should be used where the subscription was to a DataSource.\n // Alternatively, if the client doesn't maintain its subscription\n // information, they can use an element from the return value of\n // listSubscriptions(), which is of type Subscription.\n .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA)\n .addOnSuccessListener(unused -\u003e\n Log.i(TAG,\"Successfully unsubscribed.\"))\n .addOnFailureListener(e -\u003e {\n Log.w(TAG, \"Failed to unsubscribe.\");\n // Retry the unsubscribe request.\n });\n}\n```"]]