Xác định sự kiện tuỳ chỉnh
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Tính năng theo dõi hệ thống chỉ hiển thị cho bạn thông tin về các quy trình ở cấp hệ thống, vì vậy, đôi khi khó biết được phương thức nào trong ứng dụng hoặc trò chơi của bạn đang thực thi tại một thời điểm nhất định so với các sự kiện hệ thống.
Jetpack cung cấp API theo dõi mà bạn có thể dùng để gắn nhãn một phần mã cụ thể. Sau đó, thông tin này được báo cáo trong các dấu vết được ghi lại trên thiết bị.
Macrobenchmark tự động thu thập dữ liệu dấu vết bằng các điểm theo dõi tuỳ chỉnh.
Khi sử dụng công cụ dòng lệnh systrace để ghi lại dấu vết, bạn phải chọn tuỳ chọn -a
. Nếu không có tuỳ chọn này, các phương thức của ứng dụng sẽ không xuất hiện trong báo cáo theo dõi hệ thống.
Kotlin
class MyAdapter : RecyclerView.Adapter<MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): MyViewHolder {
trace("MyAdapter.onCreateViewHolder") {
MyViewHolder.newInstance(parent)
}
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
trace("MyAdapter.onBindViewHolder") {
trace("MyAdapter.queryDatabase")
val rowItem = queryDatabase(position)
dataset.add(rowItem)
}
holder.bind(dataset[position])
}
}
}
Java
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return TraceKt.trace(
"MyAdapter.onCreateViewHolder",
() -> MyViewHolder.newInstance(parent)
);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
TraceKt.trace(
"MyAdapter.onBindViewHolder",
() -> {
TraceKt.trace(
"MyAdapter.queryDatabase",
() -> {
Item rowItem = queryDatabase(position);
dataset.add(rowItem);
}
);
}
);
}
}
Bạn nên sử dụng hàm mở rộng Kotlin, ngay cả trong mã Java, vì hàm này tự động kết thúc dấu vết khi lambda hoàn tất. Điều này sẽ loại bỏ nguy cơ quên kết thúc quá trình theo dõi.
Bạn cũng có thể sử dụng API NDK cho các sự kiện theo dõi tuỳ chỉnh. Để tìm hiểu cách sử dụng API này cho mã gốc của bạn, hãy xem bài viết Sự kiện theo dõi tuỳ chỉnh trong mã gốc.
Đề xuất cho bạn
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],null,["# Define custom events\n\nSystem tracing shows you information about processes only at the system level,\nso it's sometimes difficult to know which of your app or game's methods are\nexecuting at a given time relative to system events.\n\nJetpack provides a tracing API that you can use to label a particular section of\ncode. This information is then reported in traces captured on the device.\n[Macrobenchmark](/topic/performance/benchmarking/macrobenchmark-overview)\ncaptures traces with custom trace points automatically.\n\nWhen using the systrace command line tool to capture traces, the `-a` option is\nrequired. Without this option, your app's methods don't appear in a system\ntrace report. \n\n### Kotlin\n\n```kotlin\nclass MyAdapter : RecyclerView.Adapter\u003cMyViewHolder\u003e() {\n override fun onCreateViewHolder(parent: ViewGroup,\n viewType: Int): MyViewHolder {\n trace(\"MyAdapter.onCreateViewHolder\") {\n MyViewHolder.newInstance(parent)\n }\n }\n\n override fun onBindViewHolder(holder: MyViewHolder, position: Int) {\n trace(\"MyAdapter.onBindViewHolder\") {\n trace(\"MyAdapter.queryDatabase\")\n val rowItem = queryDatabase(position)\n dataset.add(rowItem)\n }\n holder.bind(dataset[position])\n }\n }\n}\n```\n\n### Java\n\n```java\npublic class MyAdapter extends RecyclerView.Adapter\u003cMyViewHolder\u003e {\n @NonNull\n @Override\n public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {\n return TraceKt.trace(\n \"MyAdapter.onCreateViewHolder\",\n () -\u003e MyViewHolder.newInstance(parent)\n );\n }\n\n @Override\n public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {\n TraceKt.trace(\n \"MyAdapter.onBindViewHolder\",\n () -\u003e {\n TraceKt.trace(\n \"MyAdapter.queryDatabase\",\n () -\u003e {\n Item rowItem = queryDatabase(position);\n dataset.add(rowItem);\n }\n );\n }\n );\n }\n}\n```\n\nWe recommend using the Kotlin extension function, even in Java code, as it\nautomatically ends the trace when the lambda completes. This removes the risk\nof forgetting to end the tracing.\n\nYou can also use an NDK API for custom trace events. To learn about using this\nAPI for your native code, see [Custom trace events in native\ncode](/topic/performance/tracing/custom-events-native).\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [App startup time](/topic/performance/vitals/launch-time)\n- [Slow rendering](/topic/performance/vitals/render)"]]