检测"关闭/重启菜单"在android中使用辅助功能服务?

时间:2016-02-19 11:11:46

标签: android xml text-to-speech accessibilityservice

我已经创建了我的辅助功能服务,我只想在"关闭"之后拨打OnAccessibilityEvent()。出现菜单,我的目标是调用TTS引擎,只有当屏幕上显示此菜单时才能进行电话通话。

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

这是我的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" />

非常感谢你!

1 个答案:

答案 0 :(得分:1)

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

您可以收听窗口内容更改事件,我相信常量为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 :)
}

问题是每个电源开/关菜单可能不同。因此,此方法仅适用于特定的启动器应用程序。然而,即便如此,有人可能会在另一个应用程序中创建一个相同的模态。因此,即使在给定的启动器/设备/操作系统版本设置中,您也可能会出现误报检测。