如果其他应用的辅助功能服务已启用,则Android辅助功能服务无效

时间:2016-03-03 15:39:18

标签: android accessibility android-accessibility

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

1 个答案:

答案 0 :(得分:1)

我有像你这样的问题。然后我通过这个定义了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" />