我正在为我的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();
}
}

最佳答案

我遇到了像你这样的问题。然后我由此定义了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" />

关于android - 如果其他应用程序的辅助功能开启,则Android辅助功能将无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35776518/

10-10 07:35