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 DataSource.getAppPackageName().

The data view intent has the following attributes:

  • action is set to Fitness.ACTION_VIEW
  • type is DataType.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 Intent.CATEGORY_DEFAULT from the intent filter above.

Public Constructor Summary

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 Context.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 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 Context.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 DataSource.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.