סשנים מייצגים מרווח זמן שבמהלכו משתמשים לבצע פעילות כושר. sessions API מאפשר לאפליקציה ליצור סשנים ב- חנות הכושר.
בפעילויות כושר מתמשכות שבהן המשתמש מודיע לאפליקציה כשהוא מתחיל ולסיים פעילות כושר, ניתן ליצור אימונים בזמן אמת.
אתה יכול גם להכניס פעילות לחנות הכושר לאחר פעילות כושר מסתיים או כשמייבאים נתונים וסשנים מחוץ ל-Google Fit.
יצירת סשנים בזמן אמת
כדי ליצור סשנים לפעילויות כושר גופניות מתמשכות, מבצעים את השלבים הבאים:
להתחיל סשן באמצעות
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(