Unable to read data through Google Fit History API in services when app is in background or is killed?

删除回忆录丶 提交于 2019-12-14 02:50:14

问题


I am using History API of Google Fit to get the step count into my app. I have run a service and the step count is updated after a regular interval. When my app is in foreground, it works fine and I get the step count but when my app goes background, services works fine and Fitness.HistoryApi.readData does not fetch the steps. I require the steps count even when the app is in background or even when the app is killed.

Here is my problem code snippet:

private class InsertAndVerifyDataTask extends AsyncTask<String,String,String> {
    protected String doInBackground(String...params) {

            readRequest = queryFitnessData();


             dataReadResult =
                    Fitness.HistoryApi.readData(mClient, readRequest).await(1, TimeUnit.MINUTES);


        if(dataReadResult!=null) {
            printData(dataReadResult);
        }
        return "s";
    }
    protected void onPostExecute(String result){
        if(result.equals("s")) {
            compareData();
        }
    }
}

And on debug the method Fitness.HistoryApi.readData gives status as TIME_OUT when app is in background or killed. Please help me out, I am just a beginner in Android.


回答1:


You might want to use the Fitness.RecordingApi to keep recording your steps even when the app is killed.

If you are using the SensorsApi to get the step count, it wont work when your app is killed. RecordingApi is like telling the SDK- "Hey!, keep recording the steps for me even when I'm gone". And when the user comes back to your app, you can fetch the data using the getDailyTotal or readData.

See reference here

Warning: however,RecordingApi takes some time for update- you dont know at what time interval the data is updated- which means user might not be seeing the updated data. See here.



来源:https://stackoverflow.com/questions/35479918/unable-to-read-data-through-google-fit-history-api-in-services-when-app-is-in-ba

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