HistoryApi.ViewIntentBuilder

public static class HistoryApi.ViewIntentBuilder extends Object

Builder of intents to view data stored in Google Fit. This intent can be used when the application wants to display a more detailed view of a particular data type. Apps that can handle the data type (such as the app that inserted the data) can register for the intent.

If desired, setPreferredApplication(String) can be called to choose a specific application to handle the intent, if the application is installed on the device. This will often be the application defined in getAppPackageName().

The data view intent has the following attributes:

  • action is set to ACTION_VIEW
  • type is MIME_TYPE_PREFIX followed by the data type name. For example, vnd.google.fitness.data_type/com.google.heart_rate.bpm could be used for an intent to view heart rate.
  • extras contain the data source, start time, and end time. Each of them has a corresponding constant defined in Fitness.

An application that would like to handle History view intents should create an activity and add an intent filter to its manifest file. Here's an example activity that can display heart rate data:

 <activity android:name=".ViewHeartRateActivity" android:exported="true">
    <intent-filter>
      <action android:name="vnd.google.fitness.VIEW" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:mimeType="vnd.google.fitness.data_type/com.google.heart_rate.bpm" />
   </intent-filter>
 </activity>
 
If the application is only able to shown its own data (and not that from other apps), it can opt-out of implicit intents by removing CATEGORY_DEFAULT from the intent filter above.

Public Constructor Summary

HistoryApi.ViewIntentBuilder(Context context, DataType dataType)
Starts building an intent to view History data.

Public Method Summary

Intent
build()
Returns the built intent, which can be used with startActivity(Intent) to launch the desired Fitness activity.
HistoryApi.ViewIntentBuilder
setDataSource(DataSource dataSource)
Sets the data source to display data for, if a specific data source is desired.
HistoryApi.ViewIntentBuilder
setPreferredApplication(String packageName)
Sets a preferred application to use for this intent.
HistoryApi.ViewIntentBuilder
setTimeInterval(long start, long end, TimeUnit timeUnit)
Sets the time interval to display data for.

Inherited Method Summary

Public Constructors

public HistoryApi.ViewIntentBuilder (Context context, DataType dataType)

Starts building an intent to view History data.

Parameters
context a valid context
dataType the data type we want to visualize

Public Methods

public Intent build ()

Returns the built intent, which can be used with startActivity(Intent) to launch the desired Fitness activity.

Throws
IllegalStateException if not enough data has been passed into the Builder to build a valid intent.

public HistoryApi.ViewIntentBuilder setDataSource (DataSource dataSource)

Sets the data source to display data for, if a specific data source is desired. Otherwise, the viewer can choose any data source or use the default one.

Parameters
dataSource the specific data source we want to display data for

public HistoryApi.ViewIntentBuilder setPreferredApplication (String packageName)

Sets a preferred application to use for this intent. If the given app is installed and able to handle this intent, an explicit intent will be returned. This can be used in combination with getAppPackageName() to link back to the original application which inserted the data being displayed.

Parameters
packageName the package name for the application we want to link to
Returns
  • this builder, for chaining

public HistoryApi.ViewIntentBuilder setTimeInterval (long start, long end, TimeUnit timeUnit)

Sets the time interval to display data for. Every intent requires a valid time interval.

Parameters
start start time, inclusive, in time since epoch
end end time, exclusive, in time since epoch
timeUnit the timeUnit for start and end times