在果冻豆中没有收到Android辅助功能onAccessibilityEvent

时间:2013-06-08 00:19:17

标签: android accessibility android-4.2-jelly-bean

我正在尝试使用Jelly bean(Android版本4.1.1)在Android手机上接收辅助功能事件。

我已经看到几个解决这个问题的问题(特别是关于JB和JB之前的兼容性):

Android accessibility service detect notification

AccessibilityService is started but does not receive AccessibilityEvents on JellyBean

Android accessibility service

Android accessibility service backwards compatibility and jelly bean

根据所有这些问题(特别是最后一个)如果我不想确保向后兼容,我需要做的就是添加一行

android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"

在我的清单文件中并包含xml配置文件。

我这样做了,但是当通知栏中发布通知时,我仍然无法收到onAccessibilityEvent通知。 这是我的代码

public class NotificationService extends AccessibilityService {

private static final Logger logger = LoggerFactory.getLogger ("NotificationService");

@Override
public void onCreate()
{
    super.onCreate();
    logger.trace ("onCreate");
}

@Override
public void onDestroy()
{
    super.onDestroy();
    logger.trace ("onDestroy");
}

@Override
protected void onServiceConnected() {
    logger.trace("onServiceConnected");
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
    info.notificationTimeout = 100;
    setServiceInfo(info);
}

@Override
public void onInterrupt() {
    // TODO Auto-generated method stub
}

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    logger.trace("onAccessibilityEvent");


    if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        logger.info("notification: " + event.getText());
    }

}

}

以下是我在清单文件中声明服务的方法

<service android:name=".NotificationService"
         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>

这是我的accessibilityservice.xml

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://Schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeNotificationStateChanged"
    android:accessibilityFeedbackType="feedbackAllMask"
    android:notificationTimeout="100" />

当我运行我的应用程序时,我可以看到该服务已创建且已连接,但未收到任何AccessibilityEvents。

谢谢!

2 个答案:

答案 0 :(得分:1)

这适用于Android 4.1.1,但在Android 4.2(Jelly Bean)上失败。您还必须添加一个value-v17文件夹来解释SDK更改。请检查此解决方案https://stackoverflow.com/a/12042493/1179638

答案 1 :(得分:0)

我遇到了同样的问题,请尝试这种方法。在意图过滤器中添加<category android:name="android.accessibilityservice.category.FEEDBACK_SPOKEN" />

AccessibilityService is started but does not receive AccessibilityEvents on JellyBean