集成S Health SDK

时间:2017-01-17 06:26:27

标签: android

我使用Samsung S Health SDK开发了我的应用程序。我想在我的应用程序中添加S Health的步行,跑步和骑行跟踪。

如何添加这些功能?

1 个答案:

答案 0 :(得分:6)

我在readTodayWalkingDistance()中添加了以下属性:

private void readTodayWalkingDistance() {
    HealthDataResolver resolver = new HealthDataResolver(mStore, null);

    // Set time range from start time of today to the current time
    long startTime = getStartTimeOfToday();
    long endTime = System.currentTimeMillis();
    Filter filter = Filter.and(Filter.greaterThanEquals(HealthConstants.Exercise.START_TIME, startTime),
            Filter.lessThanEquals(HealthConstants.Exercise.START_TIME, endTime));

    HealthDataResolver.ReadRequest request = new ReadRequest.Builder()
            .setDataType(HealthConstants.Exercise.HEALTH_DATA_TYPE)
            .setProperties(new String[]{
                    HealthConstants.Exercise.EXERCISE_TYPE,
                    HealthConstants.Exercise.DURATION,
                    HealthConstants.Exercise.MAX_SPEED,
                    HealthConstants.Exercise.MEAN_SPEED,
            })
            .setFilter(filter)
            .build();

    try {
        resolver.read(request).setResultListener(mListener);
    } catch (Exception e) {
        Log.e(MainActivity.APP_TAG, e.getClass().getName() + " - " + e.getMessage());
    }
}

然后更新了ResultListener:

private final HealthResultHolder.ResultListener<ReadResult> mListener = new HealthResultHolder.ResultListener<ReadResult>() {
    @Override
    public void onResult(ReadResult result) {
        int exercise_type = 0;
        long duration = 0;
        float max_speed = 0.0f, mean_speed = 0.0f;
        Cursor c = null;

        try {
            c = result.getResultCursor();
            if (c != null) {
                while (c.moveToNext()) {

                    exercise_type += c.getInt(c.getColumnIndex(HealthConstants.Exercise.EXERCISE_TYPE));
                    duration += c.getLong(c.getColumnIndex(HealthConstants.Exercise.DURATION));
                    max_speed += c.getColumnIndex(HealthConstants.Exercise.MAX_SPEED);
                    mean_speed += c.getColumnIndex(HealthConstants.Exercise.MEAN_SPEED);
                }
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
        MainActivity.getInstance().getData(exercise_type, duration, max_speed, mean_speed);
    }
};

在HashSet MainActivity中添加了以下权限:

mKeySet.add(new PermissionKey(HealthConstants.Exercise.HEALTH_DATA_TYPE, PermissionType.READ));

在AndroidManifest文件中:

<meta-data
    android:name="com.samsung.android.health.permission.read"
    android:value="com.samsung.health.exercise" />