ייחוס הנתונים

לכל נקודה על הגרף ב-Google Fit משויך מקור נתונים. מקורות נתונים מכילים מידע שמזהה את האפליקציה או המכשיר שאוספים או משנים את הנתונים. שם החבילה של האפליקציה זמין למקורות נתונים שלא מייצגים חיישן פיזי.

Google Fit מאפשרת את הפעולות הבאות:

  • הפעלת Intent להצגת נתונים שמשויכים לאפליקציה ספציפית.
  • קבלת אובייקטים מסוג Intent כדי להציג נתונים באמצעות האפליקציה.
  • לגלות איזו אפליקציה הוסיפה סשן. למידע נוסף, ראו עבודה עם הפעלות.

זיהוי האפליקציה שהוסיפה נקודה על הגרף

כדי למצוא את שם החבילה של האפליקציה שהכניסה נקודה על הגרף, קודם צריך לקרוא ל-DataPoint.getOriginalDataSource על מנת למצוא את מקור הנתונים, ואז לקרוא ל-method 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-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 יחיד. מסנן כוונת האפליקציה של האפליקציה צריך לכלול את כל סוגי הנתונים שנתמכים באפליקציה, כולל סוגי נתונים מותאמים אישית.

כוונות הכושר כוללות את התוספות הבאות:

  • 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();

מידע נוסף על השימוש במסנני כוונות וכוונות