Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release thay vì aosp-main để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
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.
Trong Android 11, chế độ ngủ không chú ý là một tính năng tiết kiệm pin cho phép đặt thời gian chờ khi người dùng không hoạt động, sau đó màn hình sẽ tắt, ngay cả khi các cửa sổ có FLAG_KEEP_SCREEN_ON hiển thị hoặc khoá chế độ thức ở cấp FULL_WAKE_LOCK, SCREEN_BRIGHT_WAKE_LOCK hoặc SCREEN_DIM_WAKE_LOCK được giữ lại.
Tính năng này không ảnh hưởng đến các khoá chế độ thức ở cấp PARTIAL_WAKE_LOCK.
Ngay trước khi hết thời gian chờ, một thông báo có thể xuất hiện để cảnh báo người dùng rằng thiết bị sẽ chuyển sang trạng thái ngủ nếu họ không tương tác với thiết bị.
Trong ngữ cảnh này, hoạt động của người dùng đề cập đến mọi thứ kích hoạt lệnh gọi đến PowerManager#userActivity (không có cờ USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS), bao gồm nhưng không giới hạn ở:
Tương tác với màn hình cảm ứng
Nhấn nút vật lý
Sự kiện đầu vào từ một phụ kiện bên ngoài (ví dụ: bàn phím đã kết nối, điều khiển từ xa qua Bluetooth, điều khiển từ xa qua hồng ngoại)
Tương tác bằng giọng nói
Nhận một số thông báo HDMI CEC nhất định, chẳng hạn như One Touch Play
Bắt đầu một phiên truyền mới
Tuỳ chỉnh
Nếu tính năng này được bật, thiết bị sẽ hiển thị cảnh báo trên màn hình sau một khoảng thời gian không hoạt động nhất định của người dùng. Nếu không có hành động nào được thực hiện, màn hình sẽ tắt. Bạn có thể tuỳ chỉnh tính năng này bằng các tuỳ chọn cấu hình sau.
Định cấu hình thời gian chờ
Để định cấu hình thời gian chờ, hãy cập nhật phần tử sau trong frameworks/base/core/res/res/values/config.xml:
config_attentiveTimeout
Chỉ định thời gian mặc định (tính bằng mili giây) mà người dùng không hoạt động sau đó màn hình sẽ tắt (ngay cả khi có chế độ khoá chế độ thức màn hình).
Đặt tại thời điểm tạo bản dựng.
Nếu giá trị nằm trong khoảng từ 0 đến config_minimumScreenOffTimeout, thì thời gian chờ sẽ được đặt thành config_minimumScreenOffTimeout để ngăn thiết bị tắt màn hình ngay sau khi thức dậy.
Mặc định: -1, tắt tính năng này.
Ghi đè thời gian chờ mặc định
Để ghi đè chế độ cài đặt thời gian chờ mặc định, hãy cập nhật phần tử sau.
Settings.Secure.ATTENTIVE_TIMEOUT
Nếu được đặt, sẽ ghi đè thời gian chờ ngủ khi không chú ý mặc định do config_attentiveTimeout đặt.
Có thể được đặt trong thời gian chạy.
Định cấu hình thời lượng trước khi cảnh báo xuất hiện
Để định cấu hình thời lượng, hãy cập nhật phần tử sau trong frameworks/base/core/res/res/values/config.xml:
config_attentiveWarningDuration
Khoảng thời gian hiển thị thông báo cảnh báo cho người dùng trước khi màn hình tắt sau khi người dùng không hoạt động trong thời gian dài.
Giá trị này phải thấp hơn nhiều so với thời gian chờ khi không chú ý đã đặt, nếu không, hộp thoại cảnh báo sẽ liên tục hiển thị và không thể đóng được.
Mặc định: 30000 (30 giây).
Hiển thị các tuỳ chọn thời gian chờ trong TvSettings
Để hiển thị các lựa chọn ưu tiên về thời gian chờ, hãy cập nhật phần tử sau trong packages/apps/TvSettings/Settings/res/values/config.xml:
config_show_standby_timeout
Liệu có hiện một mục ưu tiên để cho phép tắt màn hình trong khi phát nội dung nghe nhìn hay không.
Mặc định: false.
Tài nguyên cho giao diện người dùng cảnh báo
Bố cục của hộp thoại cảnh báo được xác định trong frameworks/base/packages/SystemUI/res/layout/inattentive_sleep_warning.xml.
Các chuỗi sau đây cho hộp thoại được xác định trong frameworks/base/packages/SystemUI/res/values/strings.xml và frameworks/base/packages/SystemUI/res-product/values/strings.xml.
inattentive_sleep_warning_title
inattentive_sleep_warning_message
Bạn có thể thay đổi cấu hình và tài nguyên thời gian xây dựng bằng lớp phủ tài nguyên.
Triển khai
Bật tính năng này bằng cách sau.
Ghi đè config_attentiveTimeout mặc định.
Nếu sử dụng TvSettings AOSP:
Tắt tính năng này trong phần cài đặt bằng cách ghi đè config_show_standby_timeout.
Triển khai chế độ cài đặt của riêng bạn để đặt Settings.Secure.ATTENTIVE_TIMEOUT.
Xác nhận kết quả
Các chương trình kiểm thử CTS cho tính năng này nằm ở cts/hostsidetests/os/src/android/os/cts/InattentiveSleepTests.java.
Ví dụ và nguồn
frameworks/base/packages/SystemUI/src/com/android/systemui/power/InattentiveSleepWarningView.java chứa cách triển khai giao diện người dùng cảnh báo mặc định.
packages/apps/TvSettings cung cấp ví dụ về cách hiển thị tính năng này trong phần cài đặt.
Ví dụ về trường hợp kiểm thử thủ công
Đảm bảo chế độ cài đặt cho nhà phát triển stay_on_while_plugged_in đang tắt nếu HAL trạng thái của thiết bị báo cáo rằng thiết bị có pin (battery_present là true) vì điều này có thể ngăn tính năng tắt màn hình. adb shell settings put global stay_on_while_plugged_in 0
Đặt thời gian chờ khi người dùng không chú ý nhiều hơn vài giây so với thời lượng của hộp thoại cảnh báo. adb shell settings put secure attentive_timeout 32000
Bắt đầu phát video (để có được khoá chế độ thức màn hình).
Xác minh rằng hộp thoại cảnh báo về giấc ngủ xuất hiện sau vài giây.
Xác minh rằng màn hình tắt sau khi hết thời gian chờ đã đặt.
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,["# TV standby\n\nIn Android 11, inattentive sleep is a power-saving feature\nthat allows a user inactivity timeout to be set after which the screen turns off, even\nif windows with [FLAG_KEEP_SCREEN_ON](https://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_KEEP_SCREEN_ON)\nare visible or wakelocks of level\n[FULL_WAKE_LOCK](https://developer.android.com/reference/android/os/PowerManager#FULL_WAKE_LOCK),\n[SCREEN_BRIGHT_WAKE_LOCK](https://developer.android.com/reference/android/os/PowerManager#SCREEN_BRIGHT_WAKE_LOCK) or\n[SCREEN_DIM_WAKE_LOCK](https://developer.android.com/reference/android/os/PowerManager#SCREEN_DIM_WAKE_LOCK)\nare held.\nWakelocks with level [PARTIAL_WAKE_LOCK](https://developer.android.com/reference/android/os/PowerManager#PARTIAL_WAKE_LOCK) aren't affected by this feature.\nShortly before the timeout expires, a message can be shown that warns\nthe user that the device will go to sleep if they don't interact with the device.\n\nIn this context, user activity refers to anything that triggers a call to\n`PowerManager#userActivity` (without the `USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS`\nflag), including but not limited to:\n\n- Interacting with the touchscreen\n- Pressing a physical button\n- Input events from an external accessory (for example, connected keyboard, bluetooth remote, IR remote)\n- Voice interaction\n- Receiving certain HDMI CEC messages, such as One Touch Play\n- Starting a new cast session\n\nCustomization\n-------------\n\nIf the feature is enabled, the device shows an onscreen warning after a specified time of\nuser inactivity. If no action is taken, the screen turns off. You can customize the feature\nusing these configuration options.\n| **Caution:** If a similar power-saving feature is already implemented, enabling this feature might lead to unintended consequences, such as duplicate warning UI.\n\n### Configure the timeout\n\nTo configure the timeout, update the following element in\n`frameworks/base/core/res/res/values/config.xml`:\n\n- `config_attentiveTimeout`\n - Specifies the default time in milliseconds of user inactivity after which the screen turns off (even if screen wakelocks are in place).\n - Set at build time.\n - If the value is between `0` and `config_minimumScreenOffTimeout`, the timeout is set to `config_minimumScreenOffTimeout` to prevent the device from turning off its screen shortly after waking up.\n - Default: `-1`, which disables this feature.\n\n### Override the default timeout\n\nTo override the default timeout setting, update the following element.\n\n- `Settings.Secure.ATTENTIVE_TIMEOUT`\n - If set, overrides the default inattentive sleep timeout set by `config_attentiveTimeout`.\n - Can be set at runtime.\n\n### Configure the duration before warning appears\n\nTo configure the duration, update the following element in\n`frameworks/base/core/res/res/values/config.xml`:\n\n- `config_attentiveWarningDuration`\n - How long to show a warning message to the user before the screen turns off after prolonged user inactivity.\n - The value should be well below the set inattentive sleep timeout, otherwise the warning dialog shows constantly and can't be dismissed.\n - Default: `30000` (30s).\n\n### Show the timeout preferences in TvSettings\n\nTo show the timeout preferences, update the following element in\n`packages/apps/TvSettings/Settings/res/values/config.xml`:\n\n- `config_show_standby_timeout`\n - Whether to show a preference item for allowing turning the screen off during media playback.\n - Default: `false`.\n\n### Resources for the warning UI\n\n- The layout of the warning dialog is defined in `frameworks/base/packages/SystemUI/res/layout/inattentive_sleep_warning.xml`.\n- The following strings for the dialog are defined in `frameworks/base/packages/SystemUI/res/values/strings.xml` and `frameworks/base/packages/SystemUI/res-product/values/strings.xml`.\n - `inattentive_sleep_warning_title`\n - `inattentive_sleep_warning_message`\n\nThe build time configurations and resources can be changed by resource overlays.\n\nImplementation\n--------------\n\nEnable the feature using the following.\n\n1. Override the default `config_attentiveTimeout`.\n2. If using the AOSP `TvSettings`:\n - Disable the feature in settings by overriding `config_show_standby_timeout`.\n - Implement your own settings that set `Settings.Secure.ATTENTIVE_TIMEOUT`.\n\nValidation\n----------\n\nThe CTS tests for the feature are at\n`cts/hostsidetests/os/src/android/os/cts/InattentiveSleepTests.java`.\n\nExamples and source\n-------------------\n\n- `frameworks/base/packages/SystemUI/src/com/android/systemui/power/InattentiveSleepWarningView.java` contains the default warning UI implementation.\n- `packages/apps/TvSettings` provides an example of how to expose the feature in settings.\n\nManual test case example\n------------------------\n\n1. Make sure the `stay_on_while_plugged_in`\n developer setting is off if the device's health HAL reports that the device has a\n battery (`battery_present` is `true`) as this might prevent the\n feature from turning off the screen. \n\n `adb shell settings put global stay_on_while_plugged_in 0`\n\n2. Set an inattentive sleep timeout to be a few seconds more than the warning dialog duration. \n `adb shell settings put secure attentive_timeout 32000`\n3. Start playing back a video (to acquire a screen wakelock).\n4. Verify that the sleep warning dialog appears after a few seconds.\n5. Verify that the screen turns off after the set timeout expires."]]