บันทึกข้อมูลการออกกําลังกาย

Recording API ช่วยให้แอปของคุณขอพื้นที่เก็บข้อมูลเซ็นเซอร์แบบอัตโนมัติแบบประหยัดแบตเตอรีได้โดยสร้างการสมัครใช้บริการ การสมัครใช้บริการจะเชื่อมโยงกับแอป Android และประกอบด้วยประเภทข้อมูลการออกกําลังกายหรือแหล่งข้อมูลเฉพาะ

คุณสร้างการสมัครใช้บริการได้หลายประเภทสําหรับข้อมูลหรือแหล่งข้อมูลต่างๆ ในแอป Google Fit จะจัดเก็บข้อมูลการออกกําลังกายจากการสมัครใช้บริการที่ใช้งานอยู่ แม้ว่าแอปจะไม่ได้ทํางานอยู่และจะมีการคืนค่าการสมัครใช้บริการเหล่านี้เมื่อรีสตาร์ทระบบ

ข้อมูลที่บันทึกไว้จะปรากฏในประวัติการออกกําลังกายของผู้ใช้ หากต้องการแสดงข้อมูลในแอปแบบเรียลไทม์ด้วย ให้ใช้ Sensors API ร่วมกับ Recording API หากต้องการบันทึก ข้อมูลการออกกําลังกายด้วยข้อมูลเมตาของเซสชัน ให้ใช้ Sessions API

สมัครใช้บริการข้อมูลการออกกําลังกาย

หากต้องการขอรวบรวมข้อมูลเซ็นเซอร์ในเบื้องหลังในแอป ให้ใช้เมธอด RecordingClient.subscribe ตามที่แสดงในข้อมูลโค้ดต่อไปนี้

Kotlin

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    // This example shows subscribing to a DataType, across all possible data
    // sources. Alternatively, a specific DataSource can be used.
    .subscribe(DataType.TYPE_STEP_COUNT_DELTA)
    .addOnSuccessListener {
        Log.i(TAG, "Successfully subscribed!")
    }
    .addOnFailureListener { e ->
        Log.w(TAG, "There was a problem subscribing.", e)
    }

Java

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        // This example shows subscribing to a DataType, across all possible
        // data sources. Alternatively, a specific DataSource can be used.
        .subscribe(DataType.TYPE_STEP_COUNT_DELTA)
        .addOnSuccessListener(unused ->
                Log.i(TAG, "Successfully subscribed!"))
        .addOnFailureListener( e ->
        Log.w(TAG, "There was a problem subscribing.", e));

หากเพิ่มการสมัครใช้บริการเรียบร้อยแล้ว Google Fit จะจัดเก็บ ข้อมูลการออกกําลังกายประเภทต่างๆ TYPE_STEP_COUNT_DELTA ไว้ในประวัติการออกกําลังกายในนามของแอป การสมัครใช้บริการนี้จะปรากฏในรายการ การสมัครใช้บริการที่ใช้งานอยู่ของแอป

หากต้องการสมัครใช้บริการข้อมูลการออกกําลังกายประเภทอื่นๆ ในแอป ให้ทําตามขั้นตอนในตัวอย่างก่อนหน้านี้ แต่จะให้ข้อมูลการออกกําลังกายประเภทอื่นทุกครั้ง

แสดงรายการการสมัครใช้บริการที่ใช้งานอยู่

หากต้องการดูรายการการสมัครใช้บริการที่ใช้งานอยู่ของแอป ให้ใช้เมธอด RecordingClient.listSubscriptions ตามที่แสดงในข้อมูลโค้ดต่อไปนี้

Kotlin

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    .listSubscriptions()
    .addOnSuccessListener { subscriptions ->
        for (sc in subscriptions) {
            val dt = sc.dataType
            Log.i(TAG, "Active subscription for data type: ${dt.name}")
        }
    }

Java

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        .listSubscriptions()
        .addOnSuccessListener(subscriptions -> {
            for (Subscription sc : subscriptions) {
                DataType dt = sc.getDataType();
                Log.i(TAG, "Active subscription for data type: ${dt.name}");
            }
    });
}

ยกเลิกการสมัครข้อมูลการออกกําลังกาย

หากต้องการหยุดรวบรวมข้อมูลเซ็นเซอร์ในแอป ให้ใช้เมธอด RecordingClient.unsubscribe ดังที่แสดงในข้อมูลโค้ดต่อไปนี้

Kotlin

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
    // This example shows unsubscribing from a DataType. A DataSource should
    // be used where the subscription was to a DataSource. Alternatively, if
    // the client doesn't maintain its subscription information, they can use
    // an element from the return value of listSubscriptions(), which is of
    // type Subscription.
    .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA)
    .addOnSuccessListener {
        Log.i(TAG,"Successfully unsubscribed.")
    }
    .addOnFailureListener { e->
        Log.w(TAG, "Failed to unsubscribe.")
        // Retry the unsubscribe request.
    }

Java

Fitness.getRecordingClient(this, GoogleSignIn.getAccountForExtension(this, fitnessOptions))
        // This example shows unsubscribing from a DataType. A DataSource
        // should be used where the subscription was to a DataSource.
        // Alternatively, if the client doesn’t maintain its subscription
        // information, they can use an element from the return value of
        // listSubscriptions(), which is of type Subscription.
        .unsubscribe(DataType.TYPE_STEP_COUNT_DELTA)
        .addOnSuccessListener(unused ->
            Log.i(TAG,"Successfully unsubscribed."))
        .addOnFailureListener(e -> {
            Log.w(TAG, "Failed to unsubscribe.");
            // Retry the unsubscribe request.
        });
}