14

I'm trying to make my app detect whenever a notification is displayed. I've enabled it in the settings app and onServiceConnected does get called, however when I create a notification or receive an e-mail through the gmail app nothing happens, onAccessibilityEvent does not get called.

Android manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.slide"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<application android:label="Slide"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">
    <activity
        android:name=".Settings"
        android:label="Slide">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".Tools"
        android:label="Slide"
        android:theme="@android:style/Theme.Translucent.NoTitleBar">
    </activity>
    <service android:name=".LocalService"/>
    <service android:name=".NotificationService"
        android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
        android:label="Slide"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.accessibilityservice.AccessibilityService"/>
        </intent-filter>
    </service>
</application>

NotificationService.java

package com.test.slide;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.view.accessibility.AccessibilityEvent;

public class NotificationService extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    System.out.println("onAccessibilityEvent");
    if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        System.out.println("notification: " + event.getText());
    }
}
@Override
protected void onServiceConnected() {
    System.out.println("onServiceConnected");
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
    info.notificationTimeout = 100;
    info.feedbackType = AccessibilityEvent.TYPES_ALL_MASK;
    setServiceInfo(info);
}

@Override
public void onInterrupt() {
    System.out.println("onInterrupt");
}
}

Thanks for any help.

4

2 回答 2

21

如果清单中没有定义无障碍服务元数据标记,Android 4.0 及更高版本中的无障碍服务可能会出现异常行为。尝试按照以下示例定义元数据。您应该继续使用 setServiceInfo() 来保持与 4.0 之前的设备的向后兼容性。

此外,我建议指定特定于您的服务的反馈类型,而不是使用“全部”。

AndroidManifest.xml

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

res/xml/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" />

您的 feedbackType 有错误。更正如下。不过,请考虑使用更具体的反馈类型。

NotificationService.java

@Override
protected void onServiceConnected() {
    AccessibilityServiceInfo info = new AccessibilityServiceInfo();
    info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
    info.feedbackType = AccessibilityServiceInfo.FEEDBACK_ALL_MASK;
    info.notificationTimeout = 100;
    setServiceInfo(info);
}
于 2012-09-24T01:33:54.130 回答
4

使用 AccessibilityService 的应用需要获得设置>辅助功能的权限才能访问系统事件。允许来自设置的权限。这可能有效

检查这个链接

无障碍服务未启动

于 2012-11-12T06:26:26.657 回答