จุดข้อมูลทุกจุดใน 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