android Accessibility-service突然停止触发事件 [英] android Accessibility-service suddenly stopped triggering events

查看:56
本文介绍了android Accessibility-service突然停止触发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an AccessibilityService which was working fine but for some reason during development it stopped working. I can't seem to find that reason. Please have a look at my code and tell why it isn't working.

public class MyServicee extends AccessibilityService {

public static final String TAG = "volumeMaster";

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

    List<CharSequence> eventText;

    Log.v(TAG, "***** onAccessibilityEvent");

    final int eventType = event.getEventType();

    switch (eventType) {

        case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:

            break;
    }

    if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_CLICKED) {

    }



}

private String processUSSDText(List<CharSequence> eventText) {
    for (CharSequence s : eventText) {
        String text = String.valueOf(s);
        if (true) {
            return text;
        }
    }
    return null;
}

@Override
public void onInterrupt() {
    Log.v(TAG, "***** onInterrupt");
}


@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onServiceConnected() {
    Log.v(TAG, "***** onServiceConnected");

    AccessibilityServiceInfo info = getServiceInfo();
    info.eventTypes =
            AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED
                    | AccessibilityEvent.TYPE_VIEW_CLICKED
                    | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;

    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
    info.packageNames = new String[]{"com.whatsapp"};
    info.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
    setServiceInfo(info);
    super.onServiceConnected();
}

}

这是清单的相关部分:

    <service android:name=".MyServicee"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService" />
        </intent-filter>
        <meta-data android:name="android.accessibilityservice"
            android:resource="@xml/myservice" />
    </service>

这是 myserviceconfig.xml:

Here's myserviceconfig.xml:

<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewClicked|typeNotificationStateChanged|typeWindowStateChanged"
android:accessibilityFeedbackType="feedbackAllMask"
android:canRetrieveWindowContent="true"
android:accessibilityFlags="flagIncludeNotImportantViews|flagRequestFilterKeyEvents"
android:notificationTimeout="1"
android:packageNames="com.whatsapp"
android:settingsActivity="@string/app_name" />

代码尝试检测用户何时启动了通话录音服务.

The code attempts to detect when the user has started a call recording service.

推荐答案

您的配置存在一些问题:

There are a few problems with your configuration:

首先在 onServiceConnected 函数中,覆盖系统构建的可访问性信息.

First in your onServiceConnected function, you overwrite the system constructed accessibility info.

AccessibilityServiceInfo info = getServiceInfo();
info.eventTypes =
        AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED
                | AccessibilityEvent.TYPE_VIEW_CLICKED
                | AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;

info.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
info.packageNames = new String[]{"com.whatsapp"};
info.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
setServiceInfo(info);
super.onServiceConnected();

这整个代码块是不必要的,全部由 serviceConfig XML 文件中的类似行完成.忽略就行了,如果不能忽略,说明你的配置有问题,虽然除此之外的一切似乎都很好.

This entire block of code is unnecessary and is all accomplished by the similar lines in your serviceConfig XML file. Just omit it, if you can't omit it, there is a problem with your configuration, though everything outside of this seems to be fine.

现在,说到让您的 service_config xml 文件不言自明,让我们在这里讨论几行:

Now, speaking of letting your service_config xml file speak for itself, let's talk about a couple of lines in here:

android:notificationTimeout="1"

1 毫秒的通知超时基本上毫无价值.

A notification timeout of 1 MS is essentially worthless.

android:packageNames="com.whatsapp"

您真的想将无障碍事件限制在一个应用程序中吗?

Do you really want to limit accessibility events to just one application?

android:settingsActivity="@string/app_name"

对于 settingsActivity 属性来说,这是一个绝对无效的值.设置活动应该是应用程序中活动类的名称.例如:com.yourpackage.SettingsActivity.可以安全地省略此属性.

This is an absolutely invalid value for the settingsActivity property. The settings activity should be the name of an activity class within your application. EX: com.yourpackage.SettingsActivity. This property can be safely omitted.

除了这些信息之外,很容易获得处于完全陈旧状态的无障碍服务.有一个守护进程服务,在后台运行,让你的服务表单保持启动,但实际上并没有做富有成效的事情.解决此问题的唯一方法是重新启动设备.有时您甚至必须卸载软件包,然后重新启动设备,然后重新安装软件包.

Aside from this information it is fairly easy to get accessibility services in a completely stale state. Have a daemon service, running in the background, keeping your service form starting but NOT actually doing productive things. The only way to fix this is to restart your device. Sometimes you even have to uninstall your package and then restart your device and then reinstall your package.

这篇关于android Accessibility-service突然停止触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆