android Accessibility-service suddenly stopped triggering events

可紊 提交于 2019-12-22 18:01:44

问题


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();
}

}

Here's the relevant part of Manifest:

    <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>

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.


回答1:


There are a few problems with your configuration:

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();

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.

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"

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"

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.



来源:https://stackoverflow.com/questions/46466460/android-accessibility-service-suddenly-stopped-triggering-events

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!