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