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
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?
Thank you.
EDIT:
ADDED THE FOLLOWING FLAG STILL WITH NO LUCK:
info.flags = AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;
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"
/>
It worked good!