stopping Android Accessibility UI Component on softKey “HOME” Button pressed

一世执手 提交于 2020-01-02 20:29:47

问题


Created "AccessibilityService" and am able to show Hints based on the Text Entered.The Problem is i want to stop the activity service when the Host application Home Pressed. I have tried Adding

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
                  android:accessibilityEventTypes="typeAllMask|typeViewClicked|typeViewFocused"
                      android:canRequestFilterKeyEvents="true"
                    android:accessibilityFlags="flagRequestFilterKeyEvents"
                     android:description="@string/accessibility_service_description" />

With this am able to solve issue with phones which has Physical Button For "HOME". How to Stop the Accessibility service When "HOME" event for Soft Buttons Like Nexus 5.


回答1:


ANSWER TO ORIGINAL QUESTION: "Detecting when the software 'Home' button is pressed?"

As per Android documentation, Soft keyboards are not required to send hardware keyboard events and in fact the Android samples don't, so pretty much every software keyboard on the app store does not. You can only rely on the events from external Bluetooth and hardware keyboards.

Simply put, you can't reliably do what you want, the APIs don't support it.

ANSWER TO QUESTION EDIT:

Let me reiterate, THERE IS NO "RELIABLE" WAY TO DO THIS.

What you could do is listen for Screen change related events, and detect when the screen content matches the Home screen. Now, this is still going to be a very fragile solution. There is no consistent way to detect the Launcher Activity, vs any other Activity, and also no way to determine if perhaps the matcher you set up might match something that's not a Launcher Activity. But, here is what I think to be the most reasonable approach.

public void onAccessibilityEvent(AccessibilityEvent e) {

    switch (e.getEventType()) {
        case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
        case AccessibilityEvent.TYPE_VIEW_SCROLLED:
        case AccessibilityEvent.TYPE_WINDOWS_CHANGED:
        case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED: {
            if (detectHomeScreen(getRootInActiveWindow()) {
                //We are now at the "home" screen, do what you want here.
            }    
        }
    }
}
//A List of package names you know to be associated with "launcher" activities.  Populate this list in your classes static initialization block.
private static ArrayList<String> LAUNCHER_PACKAGE_NAMES;

private static boolean detectHomeScreen(AccessibilityNodeInfo rootNodeInfo) {
    return LAUNCHER_PACKAGE_NAMES.contains(rootNodeInfo.getPackageName().toString())
}

XML Configuration

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/accessibility_service_description"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFlags="flagIncludeNotImportantViews|flagReportViewIds"
    android:canRetrieveWindowContent="true"
    android:notificationTimeout="100"
    android:settingsActivity="com.example.SettingsActivity"
    />

The last one is cool. It allows you to set an Activity that will open up via the "Settings" button in the top right corner of the Accessibility Service configuration screen.




回答2:


Finally i had to Listen to HomeWatch Listener with the following code. https://gist.github.com/nfeld9807/0ab8341133c58c71f874

I was able to get the HOME Events.even though it took four days to figure out a way out. Chris had Really helped me to think in proper direction.



来源:https://stackoverflow.com/questions/43143084/stopping-android-accessibility-ui-component-on-softkey-home-button-pressed

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