如何在AccessibilityService

时间:2017-02-10 10:03:29

标签: android accessibilityservice

我正在开展一个项目,我们需要在显示电源对话框时提醒用户。要显示电源对话框(关闭/重启对话框),我们使用AccessibilityServiceperformGlobalAction(GLOBAL_ACTION_POWER_DIALOG)。但是,当显示此对话框时,即使我们在accessibilityservice.xml中放置了每个单独的eventtype,feedbacktype和flag,也没有其他事件被抛出。这是我们现在的代码:

AccessibilityService:

@Override
    public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
        Log.d("TESTING", "EVENT CODE: " + accessibilityEvent.getEventType()); //No event is happening when the power dialog is shown
        switch (accessibilityEvent.getEventType()){
            performGlobalAction(GLOBAL_ACTION_POWER_DIALOG);
        }
    }

accessibilityservice.xml:

<accessibility-service
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:packageNames="com.testingaccessibility"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:accessibilityFlags="flagDefault"
    android:canRetrieveWindowContent="true"
    android:canRequestEnhancedWebAccessibility="true"
    android:canRequestFilterKeyEvents="true"
    android:canRequestTouchExplorationMode="true"/>

正如您所看到的那样,我点击按钮打开电源对话框,可以在此提醒用户,但是当用户按下电源按钮时我无法这样做。

那么,电源对话框打开时是否可以发送辅助功能事件?

1 个答案:

答案 0 :(得分:0)

在您的辅助功能服务中,更改onAccessibilityEvent方法...

@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
    Log.d("TESTING", "EVENT CODE: " + accessibilityEvent.getEventType()); //No event is happening when the power dialog is shown
    if(accessibilityEvent.getText().equalsIgnoreCase("check for power dialog content text")){


       /***do your work***/
    }
}