android AccessibilityService 突然停止触发事件 - 请检查我的代码

android AccessibilityService suddenly stopped triggerring events - check my code please

我有一个 AccessibilityService,它工作正常,但由于某种原因在开发过程中停止工作。我似乎找不到那个原因。请查看我的代码并说明它为什么不起作用。

public class MyAccessibilityService extends AccessibilityService {

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {

    Toast.makeText(this, "event triggered", Toast.LENGTH_SHORT).show();
}

@Override
public void onServiceConnected() {
    AccessibilityServiceInfo config = new AccessibilityServiceInfo();
    config.eventTypes = AccessibilityEvent.TYPE_WINDOWS_CHANGED;
    config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;

    config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;

    setServiceInfo(config);
    super.onServiceConnected();
}

这是清单的相关部分:

 <service
        android:name=".MyAccessibilityService"
        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/myserviceconfig" />
    </service>

这里是myserviceconfig.xml:

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service android:accessibilityEventTypes="typeWindowsChanged"
android:notificationTimeout="100"
android:accessibilityFeedbackType="feedbackGeneric"
android:settingsActivity="com.example.android.apis.accessibility.TestBackActivity"
android:canRetrieveWindowContent="true"
xmlns:android="http://schemas.android.com/apk/res/android" />

该代码尝试检测用户何时启动了第三方应用程序的新Activity。

下面这个函数肯定是有问题的

@Override
public void onServiceConnected() {
    AccessibilityServiceInfo config = new AccessibilityServiceInfo();
    config.eventTypes = AccessibilityEvent.TYPE_WINDOWS_CHANGED;
    config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;

    config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;

    setServiceInfo(config);
    super.onServiceConnected();
}

行: AccessibilityServiceInfo config = new AccessibilityServiceInfo();

肯定是不合适的。通过获取一个未初始化的服务配置对象,您已经删除了您在服务配置 XML 文件中所做的任何配置。事实上,没有理由以编程方式进行任何配置,您可能应该删除此功能,并在您的服务配置中更改所有这些设置。以下任一解决方案都可以。

Fetch you services current configuration, instead of instantiating a new one.

@Override
public void onServiceConnected() {
    AccessibilityServiceInfo config = getServiceInfo();//This IS A REALLY IMPORTANT CHANGE!!! 
    //By instantiating a new "AccessibilityServiceInfo" you have overwritten all of your XML Config changes, as well as any default settings internal to Android! 
    //DON'T instantiate your own ServiceConfig objects!!!! Just don't. In fact, do everything in XML instead!

    config.eventTypes = AccessibilityEvent.TYPE_WINDOWS_CHANGED;
    config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;

    config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;

    setServiceInfo(config);
    super.onServiceConnected();
}

Just make all of these changes in your service config xml file.

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeWindowsChanged"
    android:notificationTimeout="100"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagIncludeNotImportantViews"
    android:settingsActivity="com.example.android.apis.accessibility.TestBackActivity"
    android:canRetrieveWindowContent="true" />

请注意,您在代码中添加的标志在服务配置文件中也可用!就用这个,它更容易。在此解决方案中,您应该完全删除 onServiceConnected 覆盖功能。

Finally

Android 无障碍服务的开发非常挑剔! Android Studio 并不总能很好地处理崩溃,尤其是当它涉及以可识别的可靠方式 and/or 将崩溃数据输入 LogCat 时。跟踪运行时异常可能很痛苦。确保当您遇到像这样突然的不一致时,不要按应用程序过滤 LogCat!在错误级别检查整个日志以查找堆栈跟踪和异常!此外,尝试卸载您的服务并再次 运行 它,然后检查整个 LogCat 输出。打开和关闭设备也有帮助。是的,这很痛苦!

在上述解决方案和这些注释之间,您应该能够找到您的问题。