我的辅助功能服务适用于身体背部,家庭。但是如果用户有具有软导航键的设备(如nexus 4)它将不会返回键事件这里是我的代码:我的目标是阻止后退和主页按钮操作,任何想法我怎么能这样做?
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>