1

我正在为我的 Android 应用程序使用辅助功能服务来读取网络运营商的弹出窗口。如果仅为我的应用程序打开辅助功能服务,它就可以正常工作。如果我为其他一些 Android 应用程序打开它,onAccessibilityEvent()则根本不起作用。

清单代码:

  <service
    android:name=".services.NotificationAccessibilityService"
    android:exported="false"
    android:label="@string/app_name"
    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/accessibilityservice" />
</service>

可访问性类:

public class NotificationAccessibilityService extends AccessibilityService {


@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getClassName() != null && event.getClassName().toString().equalsIgnoreCase(
            "android.app.AlertDialog") && event.getPackageName() != null
            && event.getPackageName().toString().equalsIgnoreCase("com.android.phone"))
    {
        Logger.d(TAG,"source info"+ " " + event.getClassName().toString());
        Logger.d(TAG, String.format("onAccessibilityEvent: [type] %s [class] %s [package] %s [time] %s [text] %s",
                getEventType(event), event.getClassName(), event.getPackageName(),
                event.getEventTime(), getEventText(event)));
@Override
public boolean bindService(Intent service, ServiceConnection conn, int flags) {
    return super.bindService(service, conn, flags);
}

@Override
public void onInterrupt() {
}

@Override
public void onServiceConnected()
{
        Intent iLaunch = new Intent(NotificationAccessibilityService.this, StatusAct.class);
        iLaunch.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(iLaunch);
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
    info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK;
    setServiceInfo(info);
}

private String getEventType(AccessibilityEvent event) {
    switch (event.getEventType()) {
        case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
            return "TYPE_NOTIFICATION_STATE_CHANGED";
        case AccessibilityEvent.TYPE_VIEW_CLICKED:
            return "TYPE_VIEW_CLICKED";
        case AccessibilityEvent.TYPE_VIEW_FOCUSED:
            return "TYPE_VIEW_FOCUSED";
        case AccessibilityEvent.TYPE_VIEW_LONG_CLICKED:
            return "TYPE_VIEW_LONG_CLICKED";
        case AccessibilityEvent.TYPE_VIEW_SELECTED:
            return "TYPE_VIEW_SELECTED";
        case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
            return "TYPE_WINDOW_STATE_CHANGED";
        case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
            return "TYPE_VIEW_TEXT_CHANGED";
    }
    return "default";
}

private String getEventText(AccessibilityEvent event) {
    StringBuilder sb = new StringBuilder();
    for (CharSequence s : event.getText()) {
        sb.append(s);
    }
    return sb.toString();
}
}
4

1 回答 1

2

我和你一样有问题。然后我通过这个定义了 AccessibilityService。它至少对我有用。你应该试一试。

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeWindowStateChanged"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagIncludeNotImportantViews"
    android:canRetrieveWindowContent="false"
    android:description="@string/accessibility_service_description" />
于 2017-02-27T05:18:46.367 回答