在 android 中使用无障碍服务检测 "shut-down/reboot menu"?

Detect "shut-down/reboot menu" using accessibility service in android?

我已经创建了无障碍服务,我想仅在出现 "shut down" 菜单时调用 OnAccessibilityEvent(),我的目标是调用 TTS 引擎以使 phone 只有当这个菜单出现在屏幕上时才说话。

我需要了解的是如何检测这唯一的事件。

这是我的 xml:

<?xml version="1.0" encoding="utf-8" ?> <accessibility-service xmlns:android="http://schemas.android.com/apk/res/android" android:accessibilityEventTypes="typeAllMask" android:accessibilityFlags="flagDefault" android:accessibilityFeedbackType="feedbackAllMask" android:canRetrieveWindowContent="true" />

非常感谢!

不,没有可靠的方法来做到这一点。做到这一点的唯一方法是 hacky。执行此操作的 hacky 方法如下:

您可以监听 window 内容更改事件,我相信常量是 TYPE_WINDOW_CONTENT_CHANGED

所以完整的代码是:

public void onAccessibilityEvent(AccessibilityEvent e) {

    switch (e.getEventType()) {
        case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED: {

            if (isPowerMenu(getRootInActiveWindow()) {
               doYourStuff();
            }
        }
    }
}

private boolean isPowerMenu(AccessibilityNodeInfo nodeInfo) {
    //In here assume you've been given the root accessibility 
    //node of the power menu.  It should be easy to detect specific
    //power menu views.  However, this will be very fragile, and 
    //depend on which version of the Android Launcher, OS, etc you
    //have.  Hence the comments, and no code :)
}

问题是每个电源 on/off 菜单可能不同。所以这种方法只适用于特定的启动器应用程序。然而,即便如此,某人也可能会在另一个应用程序中创建相同的模态。因此,即使在给定的 launcher/device/OS 版本设置中,您也可能会出现误报检测。