工作階段是指使用者進行健身活動的時段。工作階段 API 可讓應用程式在健身商店中建立工作階段。
如果是持續進行的健身活動,且使用者在開始和結束健身活動時通知您的應用程式,您可以即時建立工作階段。
您也可以在健身活動結束後,將課程插入健身商店 完成操作,或是從 Google Fit 以外的來源匯入資料和工作階段時。
即時建立課程
如要建立持續性健身活動的工作階段,請完成下列步驟:
使用以下應用程式啟動工作階段:
SessionsClient.startSession
敬上 方法。使用者結束健身活動時,請使用
SessionsClient.stopSession
方法停止時段。取消訂閱健身資料 您不想再使用
RecordingClient.unsubscribe
敬上 方法。
開始工作階段
如要在應用程式中啟動工作階段,請使用 SessionsClient.startSession
方法:
// 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)
}
// 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
方法:
// 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)
}
// 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
例項:
// 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()
// 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
方法,然後再建立工作階段一樣。
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)
}
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 Fit 中的活動區隔資料會指出使用者在特定時間間隔內進行哪些健身活動。活動區隔資料為
類型:com.google.activity.segment
(TYPE_ACTIVITY_SEGMENT
)
特別適合在運動期間支援暫停
舉例來說,如果您使用 Session.Builder.setActivity()
方法建立 30 分鐘的跑步工作階段,但使用者在其中休息 10 分鐘,應用程式就會誤報使用者跑步 30 分鐘。如果應用程式可以偵測使用者是走路還是跑步,活動區隔資料就能讓應用程式指出使用者跑步 10 分鐘、走路 10 分鐘,然後再跑步 10 分鐘。其他應用程式也能正確回報活動。
來檢視您插入的活動區隔資料
如要將活動區隔資料新增至時段,請建立包含資料點的資料集
類型為 com.google.activity.segment
。每個點都代表一段連續時間間隔,在這段時間內,使用者執行了單一活動類型。
上一個跑步和步行範例需要三個活動區隔 點:在前 10 分鐘內跑步,1 代表步行 接下來的 10 分鐘內,以及最後 10 分鐘內正在執行的時段。
// 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()
// 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
執行個體:
// 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()
// 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
敬上
方法:
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)
}
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
。
在其他應用程式中顯示工作階段
如要向使用者顯示不同應用程式中特定工作階段的詳細檢視畫面, 應用程式可叫用包含工作階段資訊的意圖。您可以指定特定應用程式,例如建立工作階段的應用程式。或者,如果 裝置上未安裝建立工作階段的應用程式,您可以允許 應用程式,顯示健身活動以回應意圖。
如要建立意圖,在其他應用程式中顯示工作階段資料,請使用 SessionsApi.ViewIntentBuilder
類別:
// 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)
// 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-filter>
<action android:name="vnd.google.fitness.VIEW"/>
<data android:mimeType="vnd.google.fitness.session/running"/>
</intent-filter>
應用程式從 Google Fit 收到的每個意圖都只有一個活動,但您可以在單一意圖篩選器中篩選多個 MIME 類型。應用程式的意圖篩選器必須納入應用程式的所有活動 支援。
健身意圖包含下列額外項目:
vnd.google.gms.fitness.start_time
vnd.google.gms.fitness.end_time
vnd.google.gms.fitness.session
您可以透過下列方式取得這些額外項目的資料:
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)
}
}
@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());
}
}