“会话”表示用户在这个时间间隔内 进行健身活动。借助 Sessions API,您的应用可以在 健身用品商店
对于用户正在进行的健身活动(用户在开始时通知应用) 并完成某项健身活动后,您可以实时创建课程。
您也可以在健身活动结束后将时段插入到健身商店中 或在您从 Google 健身外部导入数据和会话时。
实时创建会议
如要为持续性健身活动创建时段,请完成以下步骤:
使用
SessionsClient.startSession
方法。使用
SessionsClient.stopSession
方法。退订健身数据 不再使用
RecordingClient.unsubscribe
方法。
启动会话
如需在应用中启动会话,请使用 SessionsClient.startSession
方法:
Kotlin
// 1. Subscribe to fitness data // 2. Create a session object // (provide a name, identifier, description, activity and start time) val session = Session.Builder() .setName(sessionName) .setIdentifier("UniqueIdentifierHere") .setDescription("Morning run") .setActivity(FitnessActivities.RUNNING) .setStartTime(startTime, TimeUnit.MILLISECONDS) .build() // 3. Use the Sessions client to start a session: Fitness.getSessionsClient(this, googleSigninAccount) .startSession(session) .addOnSuccessListener { Log.i(TAG, "Session started successfully!") } .addOnFailureListener { e -> Log.w(TAG, "There was an error starting the session", e) }
Java
// 1. Subscribe to fitness data // 2. Create a session object // (provide a name, identifier, description, activity and start time) Session session = new Session.Builder() .setName(sessionName) .setIdentifier("UniqueIdentifierHere") .setDescription("Morning run") .setActivity(FitnessActivities.RUNNING) .setStartTime(startTime, TimeUnit.MILLISECONDS) .build(); // 3. Use the Sessions client to start a session: Fitness.getSessionsClient(this, googleSigninAccount) .startSession(session) .addOnSuccessListener(unused -> Log.i(TAG, "Session started successfully!")) .addOnFailureListener(e -> Log.w(TAG, "There was an error starting the session", e));
停止会话
如需停止应用中的会话,请使用 SessionsClient.stopSession
方法:
Kotlin
// Invoke the SessionsClient with the session identifier Fitness.getSessionsClient(this, googleSigninAccount) .stopSession(session.getIdentifier()) .addOnSuccessListener { Log.i(TAG, "Session stopped successfully!") // Now unsubscribe from the fitness data (see // Recording Fitness data) } .addOnFailureListener { e -> Log.w(TAG, "There was an error stopping the session", e) }
Java
// Invoke the SessionsClient with the session identifier Fitness.getSessionsClient(this, googleSigninAccount) .stopSession(session.getIdentifier()) .addOnSuccessListener (unused -> { Log.i(TAG, "Session stopped successfully!"); // Now unsubscribe from the fitness data (see // Recording Fitness data) }) .addOnFailureListener(e -> Log.w(TAG, "There was an error stopping the session", e));
生成的会话包含以下参数:
开始时间:您的应用调用
SessionsClient.startSession
的时间 方法。结束时间:您的应用调用
SessionsClient.stopSession
的时间 方法。名称:您传递给的
Session
对象中的名称SessionsClient.startSession
。
在健身商店中插入课程
如需插入包含之前收集的数据的会话,请执行以下操作:
创建一个
Session
对象,用于指定时间间隔和其他必需的参数 信息。创建
SessionInsertRequest
会话(可选)添加数据集并汇总数据点。
使用
SessionsClient.insertSession
方法。
插入会话
为了将包含练习元数据的健身数据插入到用户的健身数据中
历史记录,请先创建一个 SessionInsertRequest
实例:
Kotlin
// Create a session with metadata about the activity. val session = Session.Builder() .setName(SAMPLE_SESSION_NAME) .setIdentifier("UniqueIdentifierHere") .setDescription("Long run around Shoreline Park") .setActivity(FitnessActivities.RUNNING) .setStartTime(startTime, TimeUnit.MILLISECONDS) .setEndTime(endTime, TimeUnit.MILLISECONDS) .build() // Build a session insert request val insertRequest = SessionInsertRequest.Builder() .setSession(session) // Optionally add DataSets for this session. .addDataSet(dataset) .build()
Java
// Create a session with metadata about the activity. Session session = new Session.Builder() .setName(SAMPLE_SESSION_NAME) .setIdentifier("UniqueIdentifierHere") .setDescription("Long run around Shoreline Park") .setActivity(FitnessActivities.RUNNING) .setStartTime(startTime, TimeUnit.MILLISECONDS) .setEndTime(endTime, TimeUnit.MILLISECONDS) .build(); // Build a session insert request SessionInsertRequest insertRequest = new SessionInsertRequest.Builder() .setSession(session) // Optionally add DataSets for this session. .addDataSet(dataset) .build();
SessionInsertRequest
类提供了插入数据的便捷方法
转移到健身历史记录中,并在同一通话中创建一个课程
SessionsClient.insertSession
。数据集(如果有)的插入方式类似于
调用了
HistoryClient.insertData
方法,然后创建会话。
Kotlin
Fitness.getSessionsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .insertSession(insertRequest) .addOnSuccessListener { Log.i(TAG, "Session insert was successful!") } .addOnFailureListener { e -> Log.w(TAG, "There was a problem inserting the session: ", e) }
Java
Fitness.getSessionsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .insertSession(insertRequest) .addOnSuccessListener (unused -> Log.i(TAG, "Session insert was successful!")) .addOnFailureListener(e -> Log.w(TAG, "There was a problem inserting the session: ", e));
插入活动细分
Google 健身中的活动细分数据可表明哪些健身活动
用户在给定时间间隔内的表现。活动细分数据现为
类型为com.google.activity.segment
(TYPE_ACTIVITY_SEGMENT
)
尤其有助于在锻炼期间暂停锻炼。
例如,如果您创建一个 30 分钟的跑步课程,
Session.Builder.setActivity()
方法,但如果用户在其中休息 10 分钟,您的应用将
错误地显示用户跑步 30 分钟。但前提是您的应用
通过活动细分数据,您可以
应用指示用户跑步 10 分钟,步行 10 分钟,然后跑步
10 分钟。其他应用也能正确报告活动
来了解您插入的活动细分数据
如要将活动细分数据添加到时段,请创建包含数据点的数据集
类型为 com.google.activity.segment
。这些点中的每一个都代表
用户进行单一活动类型的连续时间间隔。
上一个跑步和步行示例需要三个活动细分 分:一分表示跑步前 10 分钟,一分表示步行 还有一次是在最后 10 分钟内跑步
Kotlin
// Create a DataSet of ActivitySegments to indicate the runner walked for // 10 minutes in the middle of a run. val activitySegmentDataSource = DataSource.Builder() .setAppPackageName(this.packageName) .setDataType(DataType.TYPE_ACTIVITY_SEGMENT) .setStreamName(SAMPLE_SESSION_NAME + "-activity segments") .setType(DataSource.TYPE_RAW) .build() val firstRunningDp = DataPoint.builder(activitySegmentDataSource) .setActivityField(Field.FIELD_ACTIVITY, FitnessActivities.RUNNING) .setTimeInterval(startTime, startWalkTime, TimeUnit.MILLISECONDS) .build() val walkingDp = DataPoint.builder(activitySegmentDataSource) .setActivityField(Field.FIELD_ACTIVITY, FitnessActivities.WALKING) .setTimeInterval(startWalkTime, endWalkTime, TimeUnit.MILLISECONDS) .build() val secondRunningDp = DataPoint.builder(activitySegmentDataSource) .setActivityField(Field.FIELD_ACTIVITY, FitnessActivities.RUNNING) .setTimeInterval(endWalkTime, endTime, TimeUnit.MILLISECONDS) .build() val activitySegments = DataSet.builder(activitySegmentDataSource) .addAll(listOf(firstRunningDp, walkingDp, secondRunningDp)) .build() // Create a session with metadata about the activity. val session = Session.Builder() .setName(SAMPLE_SESSION_NAME) .setDescription("Long run around Shoreline Park") .setIdentifier("UniqueIdentifierHere") .setActivity(FitnessActivities.RUNNING) .setStartTime(startTime, TimeUnit.MILLISECONDS) .setEndTime(endTime, TimeUnit.MILLISECONDS) .build() // Build a session insert request val insertRequest = SessionInsertRequest.Builder() .setSession(session) .addDataSet(activitySegments) .build()
Java
// Create a DataSet of ActivitySegments to indicate the runner walked for // 10 minutes in the middle of a run. DataSource activitySegmentDataSource = new DataSource.Builder() .setAppPackageName(getPackageName()) .setDataType(DataType.TYPE_ACTIVITY_SEGMENT) .setStreamName(SAMPLE_SESSION_NAME + "-activity segments") .setType(DataSource.TYPE_RAW) .build(); DataPoint firstRunningDp = DataPoint.builder(activitySegmentDataSource) .setActivityField(Field.FIELD_ACTIVITY, FitnessActivities.RUNNING) .setTimeInterval(startTime, startWalkTime, TimeUnit.MILLISECONDS) .build(); DataPoint walkingDp = DataPoint.builder(activitySegmentDataSource) .setActivityField(Field.FIELD_ACTIVITY, FitnessActivities.WALKING) .setTimeInterval(startWalkTime, endWalkTime, TimeUnit.MILLISECONDS) .build(); DataPoint secondRunningDp = DataPoint.builder(activitySegmentDataSource) .setActivityField(Field.FIELD_ACTIVITY, FitnessActivities.RUNNING) .setTimeInterval(endWalkTime, endTime, TimeUnit.MILLISECONDS) .build(); DataSet activitySegments = DataSet.builder(activitySegmentDataSource) .addAll(Arrays.asList(firstRunningDp, walkingDp, secondRunningDp)) .build(); // Create a session with metadata about the activity. Session session = new Session.Builder() .setName(SAMPLE_SESSION_NAME) .setDescription("Long run around Shoreline Park") .setIdentifier("UniqueIdentifierHere") .setActivity(FitnessActivities.RUNNING) .setStartTime(startTime, TimeUnit.MILLISECONDS) .setEndTime(endTime, TimeUnit.MILLISECONDS) .build(); // Build a session insert request SessionInsertRequest insertRequest = new SessionInsertRequest.Builder() .setSession(session) .addDataSet(activitySegments) .build();
使用时段读取健身数据
利用 Sessions API,您可以从健身存储区获取 符合某些条件。例如,您可以获取 或按名称或 ID 获取特定会话。你还可以指定 您是否对由您的应用或任何应用创建的会话感兴趣。
要获取符合某些条件的会话列表,
SessionReadRequest
实例:
Kotlin
// Use a start time of 1 week ago and an end time of now. val endTime = LocalDateTime.now().atZone(ZoneId.systemDefault()) val startTime = endTime.minusWeeks(1) // Build a session read request val readRequest = SessionReadRequest.Builder() .setTimeInterval(startTime.toEpochSecond(), endTime.toEpochSecond(), TimeUnit.SECONDS) .read(DataType.TYPE_SPEED) .setSessionName(SAMPLE_SESSION_NAME) .build()
Java
// Use a start time of 1 week ago and an end time of now. ZonedDateTime endTime = LocalDateTime.now().atZone(ZoneId.systemDefault()) ZonedDateTime startTime = endTime.minusWeeks(1) // Build a session read request SessionReadRequest readRequest = new SessionReadRequest.Builder() .setTimeInterval(startTime.toEpochSecond(), endTime.toEpochSecond(), TimeUnit.SECONDS) .read(DataType.TYPE_SPEED) .setSessionName(SAMPLE_SESSION_NAME) .build();
然后使用
SessionsClient.readSession
方法:
Kotlin
Fitness.getSessionsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .readSession(readRequest) .addOnSuccessListener { response -> // Get a list of the sessions that match the criteria to check the result. val sessions = response.sessions Log.i(TAG, "Number of returned sessions is: ${sessions.size}") for (session in sessions) { // Process the session dumpSession(session) // Process the data sets for this session val dataSets = response.getDataSet(session) for (dataSet in dataSets) { // ... } } } .addOnFailureListener { e -> Log.w(TAG,"Failed to read session", e) }
Java
Fitness.getSessionsClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions)) .readSession(readRequest) .addOnSuccessListener(response -> { // Get a list of the sessions that match the criteria to check the // result. List<Session> sessions = response.getSessions(); Log.i(TAG, "Number of returned sessions is: ${sessions.size}"); for (Session session : sessions) { // Process the session dumpSession(session); // Process the data sets for this session List<DataSet> dataSets = response.getDataSet(session); for (DataSet dataSet : dataSets) { // ... } } }) .addOnFailureListener(e -> Log.w(TAG,"Failed to read session", e));
使用时段读取睡眠数据
睡眠时段被视为不同于其他活动时段。默认情况下 读取响应仅包含活动时段,不包含睡眠时段。
如要添加睡眠时段,请使用
includeSleepSessions
方法时,SessionReadRequest
.为了同时添加活动和
会话,请使用 includeSleepSessions
和
includeActivitySessions
。
显示其他应用中的会话
如需向用户显示其他应用中特定会话的详细视图,请执行以下操作: 您的应用可以调用包含会话信息的 intent。您可以 指定某个特定应用,例如创建会话的应用。或者,如果 但设备上并未安装创建会话的应用,您可以允许任何 应用可显示健身活动以响应 intent。
如需创建 intent 以在其他应用上显示会话数据,请使用
SessionsApi.ViewIntentBuilder
类:
Kotlin
// Pass your activity object to the constructor val intent = SessionsApi.ViewIntentBuilder(this) .setPreferredApplication("com.example.someapp") // optional .setSession(session) .build() // Invoke the intent startActivity(intent)
Java
// Pass your activity object to the constructor Intent intent = new SessionsApi.ViewIntentBuilder(this) .setPreferredApplication("com.example.someapp") // optional .setSession(session) .build(); // Invoke the intent startActivity(intent);
接收来自其他应用的 intent
如需注册您的应用以接收来自其他健康与保健应用的 intent,请按以下步骤操作: 在清单中声明一个类似于以下内容的 intent 过滤器:
<intent-filter>
<action android:name="vnd.google.fitness.VIEW"/>
<data android:mimeType="vnd.google.fitness.session/running"/>
</intent-filter>
您的应用从 Google 健身收到的每个 intent 都只是一个 intent activity,但您可以在一个 intent 过滤器中过滤多个 MIME 类型。 应用的 intent 过滤器需要包含应用已控制的所有 activity 支持。
健身 intent 包括以下 extra:
vnd.google.gms.fitness.start_time
vnd.google.gms.fitness.end_time
vnd.google.gms.fitness.session
您可以按如下方式从这些 extra 中获取数据:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... val supportedType = Session.getMimeType(FitnessActivities.RUNNING) 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 session = Session.extract(intent) } }
Java
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... String supportedType = Session.getMimeType(FitnessActivities.RUNNING); 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); Session session = Session.extract(getIntent()); } }