我的用户将使用TalkBack或其他一些Accessible Service.我想在我们的应用程序中捕获onKeyEvent事件,但事件将被分派到启用的Accessibility Services.我创建了以下基本辅助功能服务.
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时,我没有得到任何回复.是否可以在TalkBack或其他此类辅助功能服务之前使用降低音量和向上事件?
谢谢.
编辑:
添加以下标志仍然没有运气:
info.flags = AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;
解决方法
@H_404_17@ 尝试在xml资源中配置这样的辅助功能服务,如果需要更多信息,请查看: 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" />
它工作得很好!