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.
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.
Mỗi điểm dữ liệu trong Google Fit đều có một nguồn dữ liệu liên kết. Nguồn dữ liệu chứa thông tin xác định ứng dụng hoặc thiết bị thu thập hoặc chuyển đổi dữ liệu. Tên gói của ứng dụng có sẵn cho các nguồn dữ liệu
không đại diện cho cảm biến vật lý.
Google Fit cho phép bạn thực hiện những việc sau:
Gọi một ý định xem dữ liệu liên kết với một ứng dụng cụ thể.
Nhận ý định hiển thị dữ liệu bằng ứng dụng của bạn.
Tìm hiểu ứng dụng nào đã chèn phiên. Để biết thêm thông tin, hãy xem phần Xử lý phiên.
Mỗi ý định mà ứng dụng của bạn nhận được từ Google Fit chỉ thuộc một loại,
nhưng bạn có thể lọc nhiều loại MIME trong một bộ lọc ý định duy nhất. Bộ lọc ý định của ứng dụng cần bao gồm tất cả các loại dữ liệu mà ứng dụng hỗ trợ, bao gồm cả các loại dữ liệu tuỳ chỉnh.
Ý định về thể dục bao gồm các tính năng bổ sung sau:
vnd.google.gms.fitness.start_time
vnd.google.gms.fitness.end_time
vnd.google.gms.fitness.data_source
Bạn có thể lấy dữ liệu từ các ứng dụng bổ sung này như sau:
Kotlin
overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)...valsupportedType=DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA)if(Intent.ACTION_VIEW==intent.action && supportedType==intent.type){// Get the intent extrasvalstartTime=Fitness.getStartTime(intent,TimeUnit.MILLISECONDS);valendTime=Fitness.getEndTime(intent,TimeUnit.MILLISECONDS)valdataSource=DataSource.extract(intent)}}
Java
@OverridepublicvoidonCreate(@NullableBundlesavedInstanceState){super.onCreate(savedInstanceState);...StringsupportedType=DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA);if(Intent.ACTION_VIEW.equals(getIntent().getAction()) && supportedType.equals(getIntent().getType()){// Get the intent extraslongstartTime=Fitness.getStartTime(getIntent(),TimeUnit.MILLISECONDS);longendTime=Fitness.getEndTime(getIntent(),TimeUnit.MILLISECONDS);DataSourcedataSource=DataSource.extract(getIntent());}}
Để lấy loại MIME cho một loại dữ liệu tuỳ chỉnh, hãy sử dụng
MIME_TYPE_PREFIX
hằng số:
// Inside your activityvalstartTime=...valendTime=...valdataSource=...valdataType=...valfitIntent=HistoryApi.ViewIntentBuilder(this,dataType).setTimeInterval(startTime,endTime,TimeUnit.MILLISECONDS).setDataSource(dataSource)// Optional if a specific data source is desired.setPreferredApplication("com.example.app")// Optional if you'd like a// specific app to handle the intent if that app is installed on the device.build()
Java
// Inside your activitylongstartTime=...longendTime=...DataSourcedataSource=...DataTypedataType=...IntentfitIntent=newHistoryApi.ViewIntentBuilder(this,dataType).setTimeInterval(startTime,endTime,TimeUnit.MILLISECONDS).setDataSource(dataSource)// Optional if a specific data source is desired.setPreferredApplication("com.example.app")// Optional if you'd like a// specific app to handle the intent if that app is installed on the device.build();
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eGoogle Fit allows developers to identify the source of fitness data, including the app or device that collected it.\u003c/p\u003e\n"],["\u003cp\u003eApps can be registered to receive intents from other health apps, enabling them to display and utilize fitness data.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can initiate intents to view specific fitness data within other apps, fostering interoperability between health platforms.\u003c/p\u003e\n"],["\u003cp\u003eData sources in Google Fit contain identifying information, including package names for apps, and are linked to individual data points.\u003c/p\u003e\n"]]],[],null,["# Data attribution\n\nEvery data point in Google Fit has an associated data source. Data sources\ncontain information that identifies the app or the device that collects or\ntransforms the data. The package name of the app is available for data sources\nthat don't represent a physical sensor.\n\nGoogle Fit lets you do the following:\n\n- Invoke an intent to view data that's associated with a specific app.\n- Receive intents to show data using your app.\n- Find out which app inserted a [session](/fit/sessions). For more information, see [Work with sessions](/fit/android/using-sessions).\n\nDetermine which app inserted a data point\n-----------------------------------------\n\nTo obtain the package name of the application that inserted a data point, first\ncall [`DataPoint.getOriginalDataSource`](/android/reference/com/google/android/gms/fitness/data/DataPoint#public-datasource-getoriginaldatasource)\nto get the data source, then call the\n[`DataSource.getAppPackageName`](/android/reference/com/google/android/gms/fitness/data/DataSource#public-string-getapppackagename)\nmethod: \n\n### Kotlin\n\n```kotlin\nval dataPoint : DataPoint = ...\nval dataSource = dataPoint.originalDataSource\nval appPkgName = dataSource.appPackageName\n```\n\n### Java\n\n```java\nDataPoint dataPoint = ...\nDataSource dataSource = dataPoint.getOriginalDataSource();\nString appPkgName = dataSource.getAppPackageName();\n```\n\nReceive intents from other apps\n-------------------------------\n\nTo register your app to receive intents from other health and wellness apps,\ndeclare an intent filter in your manifest that's similar to the following: \n\n```xml\n\u003cintent-filter\u003e\n \u003caction android:name=\"vnd.google.fitness.VIEW\" /\u003e\n \u003cdata android:mimeType=\"vnd.google.fitness.data_type/com.google.step_count.cumulative\" /\u003e\n \u003cdata android:mimeType=\"vnd.google.fitness.data_type/com.google.step_count.delta\" /\u003e\n\u003c/intent-filter\u003e\n```\n| **Note:** Don't include the `DEFAULT` category in your intent filter, unless you want your app to receive intents for data created by other apps.\n\nEach intent that your app receives from Google Fit is of only one type,\nbut you can filter for multiple MIME types in a single intent filter. Your app's\nintent filter needs to include all of the data types that your app supports,\nincluding custom data types.\n\nThe fitness intents include the following extras:\n\n- `vnd.google.gms.fitness.start_time`\n- `vnd.google.gms.fitness.end_time`\n- `vnd.google.gms.fitness.data_source`\n\nYou can obtain data from these extras as follows: \n\n### Kotlin\n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n ...\n val supportedType = DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA)\n\n if (Intent.ACTION_VIEW == intent.action && supportedType == intent.type) {\n // Get the intent extras\n val startTime = Fitness.getStartTime(intent, TimeUnit.MILLISECONDS);\n val endTime = Fitness.getEndTime(intent, TimeUnit.MILLISECONDS)\n val dataSource = DataSource.extract(intent)\n }\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onCreate(@Nullable Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n ...\n String supportedType = DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA);\n\n if (Intent.ACTION_VIEW.equals(getIntent().getAction()) && supportedType.equals(getIntent().getType())\n {\n // Get the intent extras\n long startTime = Fitness.getStartTime(getIntent(), TimeUnit.MILLISECONDS);\n long endTime = Fitness.getEndTime(getIntent(), TimeUnit.MILLISECONDS);\n DataSource dataSource = DataSource.extract(getIntent());\n }\n}\n```\n\nTo obtain the MIME type for a custom data type, use the\n[`MIME_TYPE_PREFIX`](/android/reference/com/google/android/gms/fitness/data/DataType#public-static-final-string-mime_type_prefix)\nconstant: \n\n### Kotlin\n\n```kotlin\nval supportedType = DataType.MIME_TYPE_PREFIX + \"com.company.customdatatype\"\n```\n\n### Java\n\n```java\nString supportedType = DataType.MIME_TYPE_PREFIX + \"com.company.customdatatype\";\n```\n\nInvoke an intent to view data\n-----------------------------\n\nTo invoke an intent to view data with another app, use the\n[`HistoryApi.ViewIntentBuilder`](/android/reference/com/google/android/gms/fitness/HistoryApi.ViewIntentBuilder)\nclass: \n\n### Kotlin\n\n```kotlin\n// Inside your activity\nval startTime = ...\nval endTime = ...\nval dataSource = ...\nval dataType = ...\n\nval fitIntent = HistoryApi.ViewIntentBuilder(this, dataType)\n .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)\n .setDataSource(dataSource) // Optional if a specific data source is desired\n .setPreferredApplication(\"com.example.app\") // Optional if you'd like a\n // specific app to handle the intent if that app is installed on the device\n .build()\n```\n\n### Java\n\n```java\n// Inside your activity\nlong startTime = ...\nlong endTime = ...\nDataSource dataSource = ...\nDataType dataType = ...\n\nIntent fitIntent = new HistoryApi.ViewIntentBuilder(this, dataType)\n .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS)\n .setDataSource(dataSource) // Optional if a specific data source is desired\n .setPreferredApplication(\"com.example.app\") // Optional if you'd like a\n // specific app to handle the intent if that app is installed on the device\n .build();\n```\n\nLearn more about how to use [intents and intent\nfilters](https://developer.android.com/guide/components/intents-filters)."]]