Fetch daily steps and calorie using Google Fit API

Google Fit

Anu S Pillai

The Admin at GadgetSaint.com

You may also like...

7 Responses

  1. Rohit Dhawade says:

    Very good.It’s help me a lot to integrate Google fit

  2. Ser says:

    Good tutorial, but i have a problem! When I run the application, just when I push the button to obtain the calories, the app closes without any error, what is the problem?

  3. gadgetsaint says:

    Can you post your Log?

  4. Lucian says:

    I have the same problem, the app is crashing when i open it, this is the log:

    05-23 15:29:47.350 15291-15758/com.ivan.lucian.fitbuddy31 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.ivan.lucian.fitbuddy31, PID: 15291
    java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:309)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
    at java.lang.Thread.run(Thread.java:818)
    Caused by: java.lang.IllegalStateException: Value is not in int format
    at com.google.android.gms.common.internal.zzac.zza(Unknown Source)
    at com.google.android.gms.fitness.data.Value.asInt(Unknown Source)
    at com.ivan.lucian.fitbuddy31.MainActivity$FetchCalorieAsync.doInBackground(MainActivity.java:380)
    at com.ivan.lucian.fitbuddy31.MainActivity$FetchCalorieAsync.doInBackground(MainActivity.java:370)
    at android.os.AsyncTask$2.call(AsyncTask.java:295)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
    at java.lang.Thread.run(Thread.java:818) 

    for what i understand this line is the problem:

    : totalSet.getDataPoints().get(0).getValue(Field.FIELD_CALORIES).asInt();
    **** from log (Caused by: java.lang.IllegalStateException: Value is not in int format)*****

  5. Khang Tran says:

    @Lucian: You right, problems came from there. I fixed by changing to asFloat() and change the get calories burned AsynTask to:

    private class FetchCalorieAsync extends AsyncTask {
    protected Float doInBackground(Object… params) {
    float total = 0;
    PendingResult result = Fitness.HistoryApi.readDailyTotal(mGoogleClient, DataType.TYPE_CALORIES_EXPENDED);
    DailyTotalResult totalResult = result.await(30, TimeUnit.SECONDS);
    if (totalResult.getStatus().isSuccess()) {
    DataSet totalSet = totalResult.getTotal();
    if (totalSet != null) {
    total = totalSet.isEmpty()
    ? 0
    : totalSet.getDataPoints().get(0).getValue(Field.FIELD_CALORIES).asFloat();
    } else {
    Log.w(“GoogleFit”, “There was a problem getting the calories.”);
    return total;

    protected void onPostExecute(Float aLong) {

    //Total calories burned for that day
    Log.i(“GoogleFit”, “Total calories: ” + aLong);


  6. ANAND K says:

    Are all the Google Fit APIs available for free if I use them in android apps that I will monetize ? If not, where can I find the pricing info ?

  7. Anu S Pillai says:

    I think its free and no issues monetizing the app.

Leave a Reply

Your email address will not be published.