Google Fit 中的每個資料點都有相關聯的資料來源。資料來源包含的資訊可用來識別用來收集或轉換資料的應用程式或裝置。應用程式的套件名稱適用於不代表實體感應器的資料來源。
Google Fit 可讓您:
判斷哪個應用程式已插入資料點
如要取得插入資料點的應用程式套件名稱,請先呼叫 DataPoint.getOriginalDataSource
取得資料來源,然後呼叫 DataSource.getAppPackageName
方法:
Kotlin
val dataPoint : DataPoint = ... val dataSource = dataPoint.originalDataSource val appPkgName = dataSource.appPackageName
Java
DataPoint dataPoint = ... DataSource dataSource = dataPoint.getOriginalDataSource(); String appPkgName = dataSource.getAppPackageName();
接收其他應用程式的意圖
如果想註冊應用程式以接收其他健康與保健應用程式的意圖,請在資訊清單中宣告類似下列內容的意圖篩選器:
<intent-filter> <action android:name="vnd.google.fitness.VIEW" /> <data android:mimeType="vnd.google.fitness.data_type/com.google.step_count.cumulative" /> <data android:mimeType="vnd.google.fitness.data_type/com.google.step_count.delta" /> </intent-filter>
應用程式從 Google Fit 接收的每個意圖都只是一種類型,但您可以在單一意圖篩選器中篩選多種 MIME 類型。應用程式的意圖篩選器必須包含應用程式支援的所有資料類型,包括自訂資料類型。
健身意圖包括下列額外項目:
vnd.google.gms.fitness.start_time
vnd.google.gms.fitness.end_time
vnd.google.gms.fitness.data_source
您可以按照下列步驟從這些額外項目中取得資料:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... val supportedType = DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA) if (Intent.ACTION_VIEW == intent.action && supportedType == intent.type) { // Get the intent extras val startTime = Fitness.getStartTime(intent, TimeUnit.MILLISECONDS); val endTime = Fitness.getEndTime(intent, TimeUnit.MILLISECONDS) val dataSource = DataSource.extract(intent) } }
Java
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... String supportedType = DataType.getMimeType(DataType.TYPE_STEP_COUNT_DELTA); if (Intent.ACTION_VIEW.equals(getIntent().getAction()) && supportedType.equals(getIntent().getType()) { // Get the intent extras long startTime = Fitness.getStartTime(getIntent(), TimeUnit.MILLISECONDS); long endTime = Fitness.getEndTime(getIntent(), TimeUnit.MILLISECONDS); DataSource dataSource = DataSource.extract(getIntent()); } }
如要取得自訂資料類型的 MIME 類型,請使用 MIME_TYPE_PREFIX
常數:
Kotlin
val supportedType = DataType.MIME_TYPE_PREFIX + "com.company.customdatatype"
Java
String supportedType = DataType.MIME_TYPE_PREFIX + "com.company.customdatatype";
叫用意圖來查看資料的意圖
如要叫用意圖以便與其他應用程式查看資料,請使用 HistoryApi.ViewIntentBuilder
類別:
Kotlin
// Inside your activity val startTime = ... val endTime = ... val dataSource = ... val dataType = ... val fitIntent = 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 activity long startTime = ... long endTime = ... DataSource dataSource = ... DataType dataType = ... Intent fitIntent = new 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();
進一步瞭解如何使用意圖和意圖篩選器。