本文介绍了如何从Google Fit获取睡眠数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

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

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);
    }
}

推荐答案

我刚刚更改了 startTime endTime ,现在一切正常这是我的 doInBackground :

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

这篇关于如何从Google Fit获取睡眠数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 07:09