onKeyEvent&无障碍服务 [英] onKeyEvent & Accessibility Service

查看:433
本文介绍了onKeyEvent&无障碍服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户将使用已启用话语提示"或其他一些无障碍服务".我想在我们的应用程序中捕获onKeyEvent事件,但是该事件将分派给已启用的Accessibility Services.我已经创建了以下基本的无障碍服务.

My users will be using TalkBack enabled or some other Accessible Service. I would like to capture the onKeyEvent events in our App but the event is dispatched to the enabled Accessibility Services. I have created the following basic Accessibility Service.

public class Accessibility_Service extends AccessibilityService {

    private String TAG = Accessibility_Service.class.getSimpleName();

    @Override
    public boolean onKeyEvent(KeyEvent event) {
        int action = event.getAction();
        int keyCode = event.getKeyCode();
        if (action == KeyEvent.ACTION_UP) {
            if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                Log.d("Hello", "KeyUp");
            } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                Log.d("Hello", "KeyDown");
            }
            return true;
        } else {
            return super.onKeyEvent(event);
        }
    }

    /**
     * Passes information to AccessibilityServiceInfo.
     */
    @Override
    public void onServiceConnected() {
        Log.v(TAG, "on Service Connected");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.packageNames = new String[] { "com.camacc" };
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        info.notificationTimeout = 100;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
        setServiceInfo(info);

    }// end onServiceConnected

    /**
     * Called on an interrupt.
     */
    @Override
    public void onInterrupt() {
        Log.v(TAG, "***** onInterrupt");

    }// end onInterrupt

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // TODO Auto-generated method stub

    }
}// end Accessibility_Service class

当我检查logcat时,没有任何反应.是否可以在话语提示或其他此类无障碍服务之前使用音量调低和调高事件?

When I check the logcat I am getting no response. Is it possible to consume the Volume Down and Up Events prior to TalkBack or other such Accessibility Services?

谢谢.

添加了以下没有运气的国旗:

ADDED THE FOLLOWING FLAG STILL WITH NO LUCK:

info.flags = AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;

推荐答案

尝试在xml资源中像这样配置辅助功能,如果您需要更多信息,请查看以下内容:

Try to configure the Accessibility Service like this in the xml resource, if you need more information look this: https://developer.android.com/guide/topics/ui/accessibility/services.html

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeContextClicked|typeViewClicked"
android:packageNames="com.example.andres.eventcapture"
android:accessibilityFlags="flagRequestFilterKeyEvents"
android:accessibilityFeedbackType="feedbackAllMask"
android:notificationTimeout="50"
android:canRetrieveWindowContent="true"
android:settingsActivity=""
android:canRequestFilterKeyEvents="true"
/>

效果很好!

这篇关于onKeyEvent&amp;无障碍服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆