How to get Sleep data from Google Fit

梦想的初衷 提交于 2020-01-20 09:36:06

问题


I want to get sleep data of previous day from Google Fit between 7pm(start time) to 11am(end time). Using following code I get the data after midnight. For example if Google fit has sleep time 10pm and wake up time 6am; I get it as 12am to 6am. So I don't get the sleep data before midnight. Please tell me what time should I set as start time. Here is my 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:


I just changed startTime and endTime and now it is working fine Here is my 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;
    }


来源:https://stackoverflow.com/questions/43780050/how-to-get-sleep-data-from-google-fit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!