如何从Google Fit获取睡眠数据

时间:2017-05-04 10:15:05

标签: android google-fit

我想在上午7点(开始时间)到上午11点(结束时间)之间从 Google Fit 获取前一天的睡眠数据。 使用以下代码我在午夜后获取数据。 例如,如果Google fit的睡眠时间为晚上10点,则唤醒时间为早上6点;我是凌晨12点到早上6点。所以我在午夜之前没有得到睡眠数据。 请告诉我我应该在什么时间设置为开始时间。这是我的AsyncTask:

private class InsertAndVerifyDataTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        fitSleepTime = 0;
        fitSleepStartTime = 0;
        fitWakeupTime = 0;
        super.onPreExecute();
    }

    protected Void doInBackground(Void... params) {

        Date now = new Date();
        long endTime, startTime;
        int hrOfDay;

        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        hrOfDay = cal.get(Calendar.HOUR_OF_DAY);
        cal.set(Calendar.HOUR_OF_DAY, 19);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        if (hrOfDay < 12)
            cal.add(Calendar.DAY_OF_YEAR, -1);
        startTime = cal.getTimeInMillis();
        sleepDayTime = MyApplication.sdf.format(cal.getTime());

        // cal.setTime(now);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);

        endTime = cal.getTimeInMillis();

        // Begin by creating the query.
        DataReadRequest readRequest = queryFitnessData(startTime, endTime);
        DataReadResult dataReadResult = Fitness.HistoryApi.readData(
                mClient, readRequest).await(1, TimeUnit.MINUTES);
        printData(dataReadResult);

        if (hrOfDay < 12) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(now);
            cal1.set(Calendar.HOUR_OF_DAY, 0);
            cal1.set(Calendar.MINUTE, 0);
            cal1.set(Calendar.SECOND, 0);
            startTime = cal1.getTimeInMillis();

            cal1.set(Calendar.HOUR_OF_DAY, 11);
            cal1.set(Calendar.MINUTE, 59);
            cal1.set(Calendar.SECOND, 59);

            endTime = cal1.getTimeInMillis();

            // Begin by creating the query.
            DataReadRequest readRequest1 = queryFitnessData(startTime, endTime);
            DataReadResult dataReadResult1 = Fitness.HistoryApi.readData(
                    mClient, readRequest1).await(1, TimeUnit.MINUTES);
            printData(dataReadResult1);
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (DashBoardSleepFragment.this.getView() != null) {
            getLastNightSleepData();
            setSleepData();
        }
        super.onPostExecute(result);
    }
}

1 个答案:

答案 0 :(得分:0)

我刚刚更改了 startTime endTime ,现在它工作正常 这是我的doInBackground

protected Void doInBackground(Void... params) {

        Date now = new Date();
        long endTime, startTime;
        int hrOfDay;

        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        hrOfDay = cal.get(Calendar.HOUR_OF_DAY);
        Log.e("hrOfDay", hrOfDay + "");
        cal.set(Calendar.HOUR_OF_DAY, 19);  //19 is my start time.
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.add(Calendar.DAY_OF_YEAR, -1);
        startTime = cal.getTimeInMillis();

        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(now);
        cal1.set(Calendar.HOUR_OF_DAY, 11);  //11 is my end time.
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        endTime = cal1.getTimeInMillis();

        // Begin by creating the query.
        DataReadRequest readRequest1 = queryFitnessData(startTime, endTime);
        DataReadResult dataReadResult1 = Fitness.HistoryApi.readData(
                mClient, readRequest1).await(1, TimeUnit.MINUTES);
        printData(dataReadResult1);
        return null;
    }