AccessibilityService not geting key event on softkey back,home button

痴心易碎 提交于 2019-12-11 05:49:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!