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

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