【问题标题】:Android AccesibilityService not receiving eventsAndroid AccesibilityService 未收到事件
【发布时间】:2016-09-30 05:59:29
【问题描述】:

这个问题我一直在碰壁:

此无障碍服务会读取通知并对其进行处理。起初它在我的手机 (Moto G4) 上成功运行,但后来它无法在任何智能手机上运行,​​即使这个相同的代码在模拟器上 100% 运行。

我打开了应用程序的无障碍服务,我倾向于停用它并再次激活它,以防在某些测试运行时,我重新启动手机并完成应用程序的完整安装并激活系统中的服务设置。

让我感到困惑的是,如上所述,这段代码在模拟器上运行得完美无缺,而且以前在我的手机上也是如此。 AccesibilityService 只是在某些智能手机上不接收任何事件。

已在 Moto G4、LG G2、华硕、华为...运行 Marshmallow / Lollipop(不工作)
在模拟器 API 16-19-23 上测试,它们都按预期工作

public class NotificationAccessibilityListener extends AccessibilityService {

    private AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    private static final String TAG = "NotificationAccListener";

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

        final int eventType = event.getEventType();

        if (eventType == TYPE_NOTIFICATION_STATE_CHANGED)
        {
            handleNotificationEvent(event);
        }
        else if (eventType == TYPE_WINDOW_STATE_CHANGED)
        {
            handleWindowStateEvent(event);
        }
        else {
            Utils.log("onAccessibilityEvent -> Got un-handled Event");
        }
    }

    @Override
    public void onInterrupt() {}


    @Override
    public void onServiceConnected() {
        // Set the type of events that this service wants to listen to.
        // Others won't be passed to this service.

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            info.eventTypes = TYPE_NOTIFICATION_STATE_CHANGED;
        } else {
            info.eventTypes = TYPE_NOTIFICATION_STATE_CHANGED | TYPE_WINDOW_STATE_CHANGED;
        }



        // Set the type of feedback your service will provide.
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;

        // Default services are invoked only if no package-specific ones are present
        // for the type of AccessibilityEvent generated.  This service *is*
        // application-specific, so the flag isn't necessary.  If this was a
        // general-purpose service, it would be worth considering setting the
        // DEFAULT flag.

        // info.flags = AccessibilityServiceInfo.DEFAULT;

        info.notificationTimeout = 20;

        this.setServiceInfo(info);
    }
    ...
}

这是 AndroidManifest.xml 中的声明

<service android:name=".features.services.NotificationAccessibilityListener"
            android:label="@string/app_name"
            android:enabled="true"
            android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
            android:process=":NotificationAccessibilityListener">

            <intent-filter>
                <action android:name="android.accessibilityservice.AccessibilityService" />
            </intent-filter>

            <meta-data
                android:name="android.accessibilityservice"
                android:resource="@xml/accessibility_service_config" />

</service>

无障碍服务配置xml

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:description="@string/accessibility_service_description"
    android:accessibilityEventTypes="typeNotificationStateChanged|typeWindowStateChanged"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:notificationTimeout="20"
    android:canRetrieveWindowContent="true"
    />

为什么 AccessibilityService 不再接收任何事件?

【问题讨论】:

  • 在设置-->可访问性-->您的服务名称中开启您的无障碍服务
  • 如问题I have the app's accessibility service turned on中所述
  • 我在运行 Marshmallow 的 LG G4 上遇到了同样的问题。我注意到当我禁用其他可访问性服务(在我的情况下是 Twilight)时,我的可访问性服务正常工作。您的手机上是否还激活了其他无障碍服务?
  • 嗨,Ivan,你最后是怎么解决这个问题的?我面临同样的行为,谢谢。

标签: android android-service android-notifications android-accessibility


【解决方案1】:

我3个月前遇到这个问题,你的服务运行在指定进程android:process=":NotificationAccessibilityListener",所以你的无障碍服务在某些设备上不起作用,去掉这行:

<service android:name=".features.services.NotificationAccessibilityListener"
            android:label="@string/app_name"
            android:enabled="true"
            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/accessibility_service_config" />

</service>

【讨论】:

  • 对不起,我花了 22 小时来测试这个。无论如何,我对您解决我的问题的解决方案寄予厚望:'),但它仍然存在,可访问性服务即使已打开,也不会在手机上收到任何事件,但目前在模拟器上可以接受
【解决方案2】:

有同样的问题,一直在寻找答案..(它以前有效,然后我删除了我认为不必要的东西..

我在三星上测试过这个,

android:notificationTimeout="100"

让它工作.. 我在 10 点时拥有它(对于模拟器/更快的手机来说可能还可以。但不是我拥有的手机)..

必须这样做(可能有效/无效..)


        info.eventTypes = TYPE_WINDOW_STATE_CHANGED | TYPE_WINDOW_CONTENT_CHANGED | TYPE_VIEW_TEXT_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
        info.notificationTimeout = 100;
        this.setServiceInfo(info);

【讨论】:

    猜你喜欢
    • 2019-09-11
    • 2016-12-18
    • 2019-12-22
    • 2016-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多