Android AccesibilityService未接收事件

时间:2016-09-30 05:59:29

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

我已经遇到了这个问题了一段时间了:

此Accesibility Service会读取通知并处理它们。起初它在我的手机上成功运行(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不再接收任何事件?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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);