Here are the examples of the java api class com.google.android.gms.fitness.data.Session taken from open source projects.
1. GoogleApiClientWrapper#startFitDataSession()
Project: android-play-games-in-motion
File: GoogleApiClientWrapper.java
File: GoogleApiClientWrapper.java
/** * Starts a new session for Fit data. This will take care of registering all the sensors, * recording the sensor data, and registering the data set as a session to Google Fit. * @param dataTypeSettings Types of data to listen to, in an array. * @param sessionDescription The description of the session. * @param listener The OnDataPointListener to receive sensor events. */ public void startFitDataSession(FitDataTypeSetting[] dataTypeSettings, String sessionDescription, OnDataPointListener listener) { for (FitDataTypeSetting dataTypeSetting : dataTypeSettings) { registerFitDataListener(dataTypeSetting, listener); startRecordingFitData(dataTypeSetting); } Session session = new Session.Builder().setName(SESSION_NAME).setDescription(sessionDescription).setActivity(FitnessActivities.RUNNING_JOGGING).setStartTime(System.currentTimeMillis(), TimeUnit.MILLISECONDS).build(); PendingResult<Status> pendingResult = Fitness.SessionsApi.startSession(mGoogleApiClient, session); pendingResult.setResultCallback(new FitResultCallback<Status>(this, FitResultCallback.RegisterType.SESSION, null, /* dataType */ true)); }
2. MainActivity#insertSegment()
Project: AndroidDemoProjects
File: MainActivity.java
File: MainActivity.java
@OnClick(R.id.btn_insert_segment) public void insertSegment() { if (!mGoogleApiClient.isConnected()) { Toast.makeText(this, "Not connected to Google", Toast.LENGTH_SHORT).show(); return; } Calendar calendar = Calendar.getInstance(); Date now = new Date(); calendar.setTime(now); long endTime = calendar.getTimeInMillis(); calendar.add(Calendar.MINUTE, -15); long walkEndTime = calendar.getTimeInMillis(); calendar.add(Calendar.MINUTE, -5); long walkStartTime = calendar.getTimeInMillis(); calendar.add(Calendar.MINUTE, -15); long startTime = calendar.getTimeInMillis(); float firstRunSpeed = 15; float walkSpeed = 5; float secondRunSpeed = 13; DataSource speedSegmentDataSource = new DataSource.Builder().setAppPackageName(this.getPackageName()).setDataType(DataType.TYPE_SPEED).setName("Tuts+ speed dataset").setType(DataSource.TYPE_RAW).build(); DataSource activitySegmentDataSource = new DataSource.Builder().setAppPackageName(this.getPackageName()).setDataType(DataType.TYPE_ACTIVITY_SEGMENT).setName("Tuts+ activity segments dataset").setType(DataSource.TYPE_RAW).build(); DataSet speedDataSet = DataSet.create(speedSegmentDataSource); DataSet activityDataSet = DataSet.create(activitySegmentDataSource); //Create speed data point for first run segment DataPoint firstRunSpeedDataPoint = speedDataSet.createDataPoint().setTimeInterval(startTime, walkStartTime, TimeUnit.MILLISECONDS); firstRunSpeedDataPoint.getValue(Field.FIELD_SPEED).setFloat(firstRunSpeed); speedDataSet.add(firstRunSpeedDataPoint); //Create speed data point for walking segment DataPoint walkingSpeedDataPoint = speedDataSet.createDataPoint().setTimeInterval(walkStartTime, walkEndTime, TimeUnit.MILLISECONDS); walkingSpeedDataPoint.getValue(Field.FIELD_SPEED).setFloat(walkSpeed); speedDataSet.add(walkingSpeedDataPoint); //Create speed data point for second run segment DataPoint secondRunSpeedDataPoint = speedDataSet.createDataPoint().setTimeInterval(walkEndTime, endTime, TimeUnit.MILLISECONDS); secondRunSpeedDataPoint.getValue(Field.FIELD_SPEED).setFloat(secondRunSpeed); speedDataSet.add(secondRunSpeedDataPoint); //Create activity data point for first run segment DataPoint firstRunActivityDataPoint = activityDataSet.createDataPoint().setTimeInterval(startTime, walkStartTime, TimeUnit.MILLISECONDS); firstRunActivityDataPoint.getValue(Field.FIELD_ACTIVITY).setActivity(FitnessActivities.RUNNING); activityDataSet.add(firstRunActivityDataPoint); //Create activity data point for walking segment DataPoint walkingActivityDataPoint = activityDataSet.createDataPoint().setTimeInterval(walkStartTime, walkEndTime, TimeUnit.MILLISECONDS); walkingActivityDataPoint.getValue(Field.FIELD_ACTIVITY).setActivity(FitnessActivities.WALKING); activityDataSet.add(walkingActivityDataPoint); //Create activity data point for second run segment DataPoint secondRunActivityDataPoint = activityDataSet.createDataPoint().setTimeInterval(walkEndTime, endTime, TimeUnit.MILLISECONDS); secondRunActivityDataPoint.getValue(Field.FIELD_ACTIVITY).setActivity(FitnessActivities.RUNNING); activityDataSet.add(secondRunActivityDataPoint); Session session = new Session.Builder().setName(SESSION_NAME).setIdentifier(getString(R.string.app_name) + " " + System.currentTimeMillis()).setDescription("Running in Segments").setStartTime(startTime, TimeUnit.MILLISECONDS).setEndTime(endTime, TimeUnit.MILLISECONDS).setActivity(FitnessActivities.RUNNING).build(); SessionInsertRequest insertRequest = new SessionInsertRequest.Builder().setSession(session).addDataSet(speedDataSet).addDataSet(activityDataSet).build(); PendingResult<Status> pendingResult = Fitness.SessionsApi.insertSession(mGoogleApiClient, insertRequest); pendingResult.setResultCallback(new ResultCallback<Status>() { @Override public void onResult(Status status) { if (status.isSuccess()) { Log.i("Tuts+", "successfully inserted running session"); } else { Log.i("Tuts+", "Failed to insert running session: " + status.getStatusMessage()); } } }); }