Các API của Google Fit, bao gồm cả API Google Fit REST, sẽ ngừng hoạt động vào năm 2026. Kể từ ngày 1 tháng 5 năm 2024, nhà phát triển không thể đăng ký sử dụng các API này.
valrequest=DataTypeCreateRequest.Builder()// The prefix of your data type name must match your app's package name.setName("com.packagename.appname.custom_data_type")// Add some custom fields, both int and float.addField("field1",Field.FORMAT_INT32).addField("field2",Field.FORMAT_FLOAT)// Add some common fields.addField(Field.FIELD_ACTIVITY).build()Fitness.getConfigClient(this,account).createCustomDataType(request).addOnSuccessListener{dataType->
// Use this custom data type to insert data into your app.Log.d(TAG,"Created data type: ${dataType.name}")}
Kiến trúc chuyển trạng thái đại diện (REST)
Loại dữ liệu là một thuộc tính của nguồn dữ liệu trong API REST. Để chụp
dữ liệu tuỳ chỉnh, bạn cần tạo một nguồn dữ liệu, rồi chỉ định loại dữ liệu:
Gọi API REST để tạo một nguồn dữ liệu mới. Ví dụ: FlexibilityMeasure.
Đặt một tên duy nhất cho loại dữ liệu, thể hiện gần đúng dữ liệu mà loại dữ liệu đó đang thu thập.
Chỉ định các trường của loại dữ liệu và định dạng của các trường đó.
Nếu tạo nguồn dữ liệu thành công, bạn sẽ nhận được một HTTP 200 OK
mã trạng thái phản hồi. Nội dung phản hồi chứa bản trình bày JSON của
nguồn dữ liệu, bao gồm cả tài sản datasource.dataStreamId. Sử dụng mã này
dưới dạng dataSourceId để thêm dữ liệu.
Sử dụng các loại dữ liệu tuỳ chỉnh
Android
Chuyển đổi tên của loại dữ liệu tuỳ chỉnh từ một chuỗi
(com.packagename.appname.custom_data_type) vào đối tượng DataType bằng cách sử dụng phương thức
ConfigClient.readDataType. Sử dụng đối tượng được trả về để chèn
và đọc dữ liệu tuỳ chỉnh.
Fitness.getConfigClient(this,account).readDataType("com.packagename.appname.custom_data_type").addOnSuccessListener{dataType->
// Use this custom data type to insert data into your app.Log.d(TAG,"Retrieved data type: ${dataType.name}")}
Kiến trúc chuyển trạng thái đại diện (REST)
Để thêm hoặc đọc dữ liệu tuỳ chỉnh bằng các loại dữ liệu tuỳ chỉnh, bạn cần có dữ liệu của các loại dữ liệu đó
nguồn. Để kiểm tra nguồn dữ liệu của một loại dữ liệu tuỳ chỉnh, hãy gửi GET
đến API REST.
Thêm dữ liệu tuỳ chỉnh
Để chèn dữ liệu tuỳ chỉnh, hãy tạo một tập dữ liệu chứa các điểm dữ liệu mới. Chỉ định
nguồn dữ liệu bạn đã tạo cho loại dữ liệu tuỳ chỉnh của mình. Các điểm dữ liệu cần
có tất cả các trường và định dạng chính xác được chỉ định trong loại dữ liệu tuỳ chỉnh.
Đọc dữ liệu tuỳ chỉnh
Để đọc dữ liệu tùy chỉnh, hãy chỉ định nguồn dữ liệu bạn đã tạo cho tùy chỉnh của mình
loại dữ liệu khi bạn truy xuất điểm dữ liệu từ nền tảng Google Fit.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eDevelop custom data types to capture unique fitness data specific to your app, ensuring the data type name reflects the data and uses your app's package name as a prefix.\u003c/p\u003e\n"],["\u003cp\u003eCreate custom data types in Android using the \u003ccode\u003eConfigClient.createCustomDataType\u003c/code\u003e method and in REST by creating a new data source with a unique name and defined fields.\u003c/p\u003e\n"],["\u003cp\u003eUse the \u003ccode\u003eConfigClient.readDataType\u003c/code\u003e method in Android or send a \u003ccode\u003eGET\u003c/code\u003e request to the REST API to access your custom data type for inserting and reading data.\u003c/p\u003e\n"],["\u003cp\u003eInsert custom data by creating a dataset with data points corresponding to your custom data type's fields and specifying the associated data source.\u003c/p\u003e\n"],["\u003cp\u003eRead custom data by specifying the data source linked to your custom data type when retrieving data points from the Google Fit platform.\u003c/p\u003e\n"]]],[],null,["# Custom data types\n\nIf your app needs to capture information that isn't already covered by one of the\nexisting data types on the Google Fit platform, you can create a custom data type.\n| **Note:** Custom data types are only available to the apps that created them. Other apps can't see or use your custom data types.\n\nCreating custom data types\n--------------------------\n\nCreate, or specify, a custom data type to capture custom data. When you create\ncustom data types, make sure:\n\n- The data type name accurately represents the underlying data.\n- The prefix of the data type name matches your app's package name.\n\n### Android\n\nTo create a custom data type for the first time, use the\n[`ConfigClient.createCustomDataType`](/android/gms/fitness/ConfigClient#create%0ACustomDataType(com.google.android.gms.fitness.request.DataTypeCreateRequest))\nmethod: \n\n val request = DataTypeCreateRequest.Builder()\n // The prefix of your data type name must match your app's package name\n .setName(\"com.packagename.appname.custom_data_type\") // Add some custom fields, both int and float\n .addField(\"field1\", Field.FORMAT_INT32)\n .addField(\"field2\", Field.FORMAT_FLOAT)\n // Add some common fields\n .addField(Field.FIELD_ACTIVITY)\n .build()\n\n Fitness.getConfigClient(this, account)\n .createCustomDataType(request)\n .addOnSuccessListener { dataType -\u003e\n // Use this custom data type to insert data into your app.\n Log.d(TAG, \"Created data type: ${dataType.name}\")\n }\n\n### REST\n\nData types are a property of data sources, in the REST API. To capture\ncustom data, you need to create a data source and then specify the data type:\n\n1. Call the REST API to create a *new data source* . For example, `FlexibilityMeasure`.\n2. Give the data type a unique name, that closely represents the data it's capturing.\n\n | **Note:** This name can be anything, as long as it's unique and doesn't have the same name as another data type. We recommend this convention: com.packagename.appname.custom_data_type\n3. Specify the fields of the data type, and their formats.\n\n**HTTP method** \n\n POST\n\n**Request URL** \n\n https://www.googleapis.com/fitness/v1/users/me/dataSources\n\n**Request body** \n\n {\n \"dataStreamName\": \"FlexibilityMeasure\",\n \"type\": \"raw\",\n \"application\": {\n \"detailsUrl\": \"http://recoveryapps.com\",\n \"name\": \"Stretch Flex\",\n \"version\": \"1\"\n },\n \"dataType\": {\n \"name\": \"com.recoveryapps.stretchflex.flexibility\",\n \"field\": [\n {\n \"name\": \"ankle_range_degrees\",\n \"format\": \"integer\"\n },\n {\n \"name\": \"wrist_range_degrees\",\n \"format\": \"integer\",\n \"optional\": true\n }\n ]\n }\n }\n\n**Response**\n\nIf your data source was created successfully, you'll get a [`200 OK`](https://httpstatuses.com/200) HTTP\nresponse status code. The response body contains a JSON representation of\nthe data source, including a `datasource.dataStreamId` property. Use this ID\nas the `dataSourceId` to add data.\n\nUsing custom data types\n-----------------------\n\n### Android\n\nConvert your custom data type's name from a string\n(com.packagename.appname.custom_data_type) into a `DataType` object using the\n[`ConfigClient.readDataType`](/android/reference/com/google/android/gms/fitness/ConfigClient#readDataType(java.lang.String)) method. Use the returned object to [insert](/android/reference/com/google/android/gms/fitness/HistoryClient#public-taskvoid-insertdata-dataset-dataset)\nand [read](/android/reference/com/google/android/gms/fitness/ConfigClient#public-taskdatatype-readdatatype-string-datatypename) custom data. \n\n Fitness.getConfigClient(this, account)\n .readDataType(\"com.packagename.appname.custom_data_type\")\n .addOnSuccessListener { dataType -\u003e\n // Use this custom data type to insert data into your app.\n Log.d(TAG, \"Retrieved data type: ${dataType.name}\")\n }\n\n### REST\n\nTo add or read custom data using your custom data types, you need their data\nsources. To check the data sources of a custom data type, [send a `GET`\nrequest to the REST API](/fit/rest/v1/reference/users/dataSources/list).\n\n### Adding custom data\n\nTo insert custom data, create a dataset with new data points. Specify the\ndata source you created for your custom data type. The data points need to\nhave all the correct fields and formats specified in your custom data type.\n\n### Reading custom data\n\nTo read custom data, specify the data source you created for your custom\ndata type when you retrieve data points from the Google Fit platform."]]