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

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

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

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

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

หากต้องการดูชื่อแพ็กเกจของแอปพลิเคชันที่แทรกจุดข้อมูล ก่อนอื่นให้เรียกใช้ 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 จากแอปอื่นๆ

หากต้องการลงทะเบียนแอปเพื่อรับ 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

คุณสามารถรับข้อมูลจากส่วนเสริมเหล่านี้ได้ดังนี้

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

เรียกใช้ Intent เพื่อดูข้อมูล

หากต้องการเรียกใช้ Intent เพื่อดูข้อมูลด้วยแอปอื่น ให้ใช้คลาส 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();

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