3

我想从中获取swimming数据Samsung Health App但没有得到任何适当的解决方案。下面是我的代码。

    public class SwimmingReport {

    private final HealthDataStore mStore;
    private SwimObserver swimObserver;
    private static final long ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000L;

    public SwimmingReport(HealthDataStore store) {
        mStore = store;
    }

    public void start(SwimObserver listener, String strDate, JSONObject jsonObject) {
        swimObserver = listener;
        HealthDataObserver.addObserver(mStore, HealthConstants.Exercise.HEALTH_DATA_TYPE, new HealthDataObserver(null) {
            @Override
            public void onChange(String s) {
                readTodaySwimData(strDate,jsonObject);
            }
        });

        readTodaySwimData(strDate,jsonObject);
    }

    private void readTodaySwimData(String strDate,JSONObject jsonObject) {
        HealthDataResolver resolver = new HealthDataResolver(mStore, null);
        long startTime = GlobalMethods.getEpochTime(strDate);
        long endTime = startTime + ONE_DAY_IN_MILLIS;

        HealthDataResolver.ReadRequest request = new HealthDataResolver.ReadRequest.Builder()
                .setDataType(HealthConstants.Exercise.HEALTH_DATA_TYPE)
                .setProperties(new String[]{HealthConstants.Exercise.EXERCISE_TYPE})
                .setLocalTimeRange(HealthConstants.Exercise.START_TIME, HealthConstants.Exercise.TIME_OFFSET,
                        startTime, endTime)
                .build();

        try {
            resolver.read(request).setResultListener(result ->{
                double distance = 0.0;
                try {
                    for (HealthData data : result) {
                        distance += data.getFloat(HealthConstants.Exercise.DISTANCE);
                    }
                } finally {
                    result.close();
                }
                if (swimObserver != null) {
                    swimObserver.onChanged(distance,strDate,jsonObject);
                }
            });
        } catch (Exception e) {
            Log.e("=> ", "Getting step count fails.", e);
        }
    }

    public interface SwimObserver {
        void onChanged(Double distance, String date, JSONObject jsonObject);
    }

}

是我的整个源代码

4

1 回答 1

1

我在您的代码中发现您想distance从读取结果中读取值。

distance += data.getFloat(HealthConstants.Exercise.DISTANCE);

但你描述的绝对不相关的财产。也许这就是问题所在。

.setProperties(new String[]{HealthConstants.Exercise.EXERCISE_TYPE})

尝试这个。

.setProperties(new String[]{HealthConstants.Exercise.DISTANCE})
于 2020-01-02T01:22:40.553 回答