เซสชันแสดงช่วงเวลาที่ผู้ใช้ทำกิจกรรมการออกกำลังกาย Sessions API ช่วยให้แอปของคุณสร้างเซสชันใน สโตร์การออกกำลังกายได้
สำหรับกิจกรรมการออกกำลังกายต่อเนื่องซึ่งผู้ใช้แจ้งแอปเมื่อเริ่มและทำกิจกรรมการออกกำลังกายเสร็จแล้ว คุณสามารถสร้างเซสชันแบบเรียลไทม์ได้
นอกจากนี้ คุณยังสามารถแทรกเซสชันลงในสโตร์ของฟิตเนสหลังจากที่กิจกรรมการออกกำลังกายสิ้นสุดลง หรือเมื่อคุณนำเข้าข้อมูลและเซสชันจากภายนอก Google Fit ได้ด้วย
สร้างเซสชันแบบเรียลไทม์
หากต้องการสร้างเซสชันสำหรับกิจกรรมการออกกำลังกายต่อเนื่อง ให้ทำตามขั้นตอนต่อไปนี้
สมัครรับข้อมูลข้อมูลการออกกำลังกายโดยใช้เมธอด
RecordingClient.subscribe
เริ่มเซสชันโดยใช้เมธอด
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 Fit จะบอกถึงกิจกรรมการออกกำลังกายของผู้ใช้ตลอดช่วงเวลาที่ระบุ ข้อมูลกลุ่มกิจกรรม
เป็นประเภท com.google.activity.segment
(TYPE_ACTIVITY_SEGMENT
)
และมีประโยชน์อย่างยิ่งสำหรับการรองรับการหยุดชั่วคราวระหว่างออกกำลังกาย
ตัวอย่างเช่น หากคุณสร้างเซสชันการวิ่ง 30 นาทีด้วยเมธอด Session.Builder.setActivity()
แต่ผู้ใช้หยุดพัก 10 นาทีระหว่างนั้น แอปจะแสดงให้เห็นว่าผู้ใช้วิ่งเป็นเวลา 30 นาทีอย่างไม่ถูกต้อง หากแอปของคุณตรวจจับได้ว่าผู้ใช้กำลังเดินหรือวิ่งอยู่ ข้อมูลกลุ่มกิจกรรมจะช่วยให้แอประบุว่าผู้ใช้วิ่งเป็นเวลา 10 นาที เดิน 10 นาที จากนั้นวิ่งอีก 10 นาที แอปอื่นๆ ก็สามารถรายงานกิจกรรมได้อย่างถูกต้อง
โดยดูข้อมูลกลุ่มกิจกรรมที่คุณใส่ไว้
หากต้องการเพิ่มข้อมูลกลุ่มกิจกรรมลงในเซสชัน ให้สร้างชุดข้อมูลที่มีจุดประเภท com.google.activity.segment
แต่ละจุดเหล่านี้แสดงถึงช่วงเวลาต่อเนื่องที่ผู้ใช้ทำกิจกรรมประเภทเดียว
ตัวอย่างการวิ่งและเดินก่อนหน้านี้จะต้องมีคะแนนกลุ่มกิจกรรม 3 จุด ได้แก่ ประเด็นหนึ่งสำหรับการวิ่งในช่วง 10 นาทีแรก อีกจุดหนึ่งสำหรับการเดินในช่วง 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 จะช่วยให้คุณรับรายการเซสชันจากร้านค้าฟิตเนสที่ตรงกับเกณฑ์บางอย่างได้ ตัวอย่างเช่น คุณสามารถดูเซสชันทั้งหมดที่อยู่ในช่วงเวลาที่กำหนด หรือรับเซสชันหนึ่งๆ ตามชื่อหรือรหัสได้ และยังระบุได้ด้วยว่าสนใจเข้าร่วมเซสชันที่แอปสร้างขึ้นหรือจากแอปใดก็ตาม
หากต้องการดูรายการเซสชันที่ตรงกับเกณฑ์บางอย่าง ให้สร้างอินสแตนซ์ 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 เพื่อแสดงข้อมูลเซสชันในแอปอื่น ให้ใช้คลาส 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 ในไฟล์ Manifest ที่คล้ายกับตัวอย่างต่อไปนี้
<intent-filter>
<action android:name="vnd.google.fitness.VIEW"/>
<data android:mimeType="vnd.google.fitness.session/running"/>
</intent-filter>
Intent แต่ละรายการที่แอปได้รับจาก Google Fit จะเป็นกิจกรรมเดียวเท่านั้น แต่คุณสามารถกรอง MIME หลายประเภทได้ในตัวกรอง Intent รายการเดียว ตัวกรอง Intent ของแอปต้องรวมกิจกรรมทั้งหมดที่แอปรองรับ
จุดประสงค์ของการออกกำลังกายประกอบด้วยส่วนเพิ่มเติมต่อไปนี้
vnd.google.gms.fitness.start_time
vnd.google.gms.fitness.end_time
vnd.google.gms.fitness.session
คุณสามารถรับข้อมูลจากส่วนเสริมเหล่านี้ได้ดังนี้
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()); } }