Android accessibility service detect notification

0 down vote favorite

I'm trying to make my app detect whenever a notification is displayed. I've enabled it in the settings app andonServiceConnecteddoes get called, however when I create a notification or receive an e-mail through the gmail app nothing happens,onAccessibilityEventdoes 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.

asked Sep 23 '12 at 17:18
ng93
396 1 4 20


Did you found the solution? i have exactly the same problem. –  Daniel Martinus Sep 25 '12 at 17:24

alanv's answer worked for me –  ng93 Sep 26 '12 at 12:29

For what system version are you programming? ICS? jelly bean? –  Daniel Martinus Sep 27 '12 at 11:33

Jelly bean 4.1.1 –  ng93 Oct 1 '12 at 14:56

2 Answers

up vote 2 down vote accepted

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); }
answered Sep 24 '12 at 1:33
alanv
442 1 10


Now you are defining theserviceInfoin XML and code. You may leave out the code part of AccessibilityServiceInfo because the xml is defined in the manifest. –  Daniel Martinus Sep 24 '12 at 5:57
1  
On the contrary, you must define it in both places for compatibility with pre-ICS devices. –  alanv Sep 24 '12 at 8:01
27002600_pP5a.gif
up vote 1 down vote

The app using AccessibilityService needed to have a permission from settings>Accessibility in order to access the system events. Allow permission from settings . This may work

check this link

accessibility service is not started

转载于:https://my.oschina.net/zhuzihasablog/blog/110534

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值