2

一段时间以来,我一直在遇到这个问题:

此无障碍服务读取通知并处理它们。起初它在我的手机(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 不再接收任何事件?

4

2 回答 2

0

我在 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>
于 2016-09-30T06:35:11.600 回答
0

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

我在三星上测试这个,

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);
于 2021-04-12T10:30:32.963 回答