Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

Android accessibility service detect notification

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Accessibility services in Android 4.0 and above can behave strangely if there is no accessibility-service meta-data tag defined in the manifest. Try defining the meta-data as in the examples below. You should continue to use setServiceInfo() to maintain backward compatibility with pre-4.0 devices.

Also, I would recommend specifying a feedback type that is specific to your service, rather than using "all".

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

There was an error in your feedbackType. Corrected below. Still, consider using a more specific feedback type.

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...