问题
My AccessibilityService work fine for physical back,home. but if user have device with soft navigation key (like nexus 4 ) it will not return key event here is my code : my goal is to block back and home button action, any idea how i can do it?
Accessibility_Service.java
public class Accessibility_Service extends AccessibilityService {
private SharedPreferences prefs;
private String TAG = "Accessibility_Service";
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(TAG,"AccessibilityEvent: "+event.toString());
}
@Override
public void onInterrupt() {
Log.d(TAG,"onInterrupt: ");
}
@Override
protected boolean onKeyEvent(KeyEvent event) {
try {
Log.d(TAG,"key: "+event.getKeyCode());
if (prefs.getBoolean(MyStandout.IS_RUNNNING_PREF, false)) {
if (handerEvent(event)) {
return true;
}
}else {
Log.d(TAG,"standOutNotRuning");
}
} catch (Exception e) {
e.printStackTrace();
}
return super.onKeyEvent(event);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onServiceConnected() {
prefs = getSharedPreferences(MyStandout.MY_PREFS_NAME, MODE_PRIVATE);
AccessibilityServiceInfo tempInfo = getServiceInfo();
tempInfo.flags |= AccessibilityServiceInfo.FLAG_REQUEST_FILTER_KEY_EVENTS;
tempInfo.flags |= AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
tempInfo.flags |= AccessibilityServiceInfo.FLAG_REQUEST_TOUCH_EXPLORATION_MODE;
setServiceInfo(tempInfo);
}
config.xml
<accessibility-service
android:accessibilityFlags="flagRequestFilterKeyEvents|flagIncludeNotImportantViews|flagRequestTouchExplorationMode"
android:description="@string/accessibility_service_description"
android:settingsActivity="com.example.ameerhamza.lockscreentouch.Accessibility_Service"
android:canRequestFilterKeyEvents="true"
xmlns:android="http://schemas.android.com/apk/res/android"
/>
manifast
<service
android:name="com.example.ameerhamza.lockscreentouch.Accessibility_Service"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/serviceconfig" />
</service>
来源:https://stackoverflow.com/questions/49494076/accessibilityservice-not-geting-key-event-on-softkey-back-home-button