[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Device-side metrics collectors\n\nWhen running device-side tests (for example, instrumentations, UI Automator\ntests), host-side collectors might not be ideal because it's difficult to\nsynchronize metric collection to a test running on a device. For example, a\nscreenshot taken asynchronously is likely to miss the wanted screen and be\nuseless.\n\nTo meet these use cases, a device-side version of our collectors exists\nand can be used in any `AndroidJUnitRunner` instrumentation.\n[`BaseMetricListener`](https://android.googlesource.com/platform/platform_testing/+/refs/heads/android16-release/libraries/device-collectors/src/main/java/android/device/collectors/BaseMetricListener.java)\ncan be implemented to automatically report metrics that are collected\nin a way fully compatible with the Tradefed reporting pipeline.\n\nThis library is decoupled from Tradefed itself and can be used without Tradefed.\n\nIf you're using the [`AndroidJUnitTest`](https://android.googlesource.com/platform/tools/tradefederation/+/refs/heads/android16-release/test_framework/com/android/tradefed/testtype/AndroidJUnitTest.java)\nrunner from Tradefed, you can specify the following command line option\nto have your collector running with your tests: \n\n --device-listeners android.device.collectors.ScreenshotListener\n\n**Caution:** In order for the collector classes to be resolved at runtime, your instrumentation APK needs to statically include them by adding the following to your makefile: \n\n LOCAL_STATIC_JAVA_LIBRARIES += collector-device-lib\n\nImplementation\n--------------\n\nWhen implementing on top of the base class `BaseMetricListener`, you can choose\nwhen you would like to collect your metrics during the lifecycle of the\ninstrumentation:\n\n- When a test run starts: `onTestRunStart`\n- When a test case starts: `onTestStart`\n- When a test case ends: `onTestEnd`\n- When a test case fails: `onTestFail`\n- When a test run ends: `onTestRunEnd`\n\nInteraction\n-----------\n\nThe collection of metrics on the device side is made synchronously to the\ninstrumentation execution itself, and metrics are passed back to the\ninstrumentation results and parsed by Tradefed to be reported as part of the\ninvocation."]]