การระบุแหล่งที่มาของข้อมูล

จุดข้อมูลทุกจุดใน Google Fit มีแหล่งข้อมูลที่เชื่อมโยง แหล่งข้อมูล มีข้อมูลที่ระบุถึงแอปหรืออุปกรณ์ที่รวบรวมหรือ แปลงข้อมูลได้ ชื่อแพ็กเกจของแอปพร้อมใช้งานสำหรับแหล่งข้อมูล ที่ไม่ได้แสดงถึงเซ็นเซอร์ทางกายภาพ

Google Fit ช่วยให้คุณทำสิ่งต่อไปนี้ได้

  • เรียกใช้ความตั้งใจเพื่อดูข้อมูลที่เชื่อมโยงกับแอปใดแอปหนึ่ง
  • รับ Intent เพื่อแสดงข้อมูลโดยใช้แอป
  • ค้นหาว่าแอปใดแทรกเซสชัน สำหรับข้อมูลเพิ่มเติม โปรดดูข้อมูลที่หัวข้อใช้งานเซสชัน

ระบุแอปที่แทรกจุดข้อมูล

หากต้องการรับชื่อแพ็กเกจของแอปพลิเคชันที่แทรกจุดข้อมูล ก่อนอื่นให้ โทรหา DataPoint.getOriginalDataSource เพื่อรับแหล่งข้อมูล จากนั้นเรียกใช้ DataSource.getAppPackageName วิธีการ:

KotlinJava
val dataPoint : DataPoint = ...
val dataSource = dataPoint.originalDataSource
val appPkgName = dataSource.appPackageName
DataPoint dataPoint = ...
DataSource dataSource = dataPoint.getOriginalDataSource();
String appPkgName = dataSource.getAppPackageName();

รับ Intent จากแอปอื่นๆ

หากต้องการลงทะเบียนแอปเพื่อรับ Intent จากแอปสุขภาพและความเป็นอยู่ที่ดีอื่นๆ ประกาศตัวกรอง Intent ในไฟล์ Manifest ซึ่งคล้ายกับตัวอย่างต่อไปนี้

<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>

Intent แต่ละรายการที่แอปได้รับจาก Google Fit จะมีเพียงประเภทเดียวเท่านั้น แต่คุณสามารถกรอง MIME หลายประเภทได้ในตัวกรอง Intent เดียว แอปของคุณ ตัวกรอง Intent ต้องรวมข้อมูลทุกประเภทที่แอปรองรับ รวมถึงประเภทข้อมูลที่กำหนดเอง

จุดประสงค์ในการออกกำลังกายมีดังต่อไปนี้

  • vnd.google.gms.fitness.start_time
  • vnd.google.gms.fitness.end_time
  • vnd.google.gms.fitness.data_source

คุณสามารถรับข้อมูลจากรายการเพิ่มเติมเหล่านี้:

KotlinJava
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)
   
}
}
@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 ค่าคงที่:

KotlinJava
val supportedType = DataType.MIME_TYPE_PREFIX + "com.company.customdatatype"
String supportedType = DataType.MIME_TYPE_PREFIX + "com.company.customdatatype";

เรียกใช้ความตั้งใจเพื่อดูข้อมูล

หากต้องการเรียกใช้ความตั้งใจที่จะดูข้อมูลด้วยแอปอื่น ให้ใช้ HistoryApi.ViewIntentBuilder ชั้นเรียน:

KotlinJava
// 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()
// 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();

ดูข้อมูลเพิ่มเติมเกี่ยวกับวิธีใช้ความตั้งใจและความตั้งใจ ตัวกรอง