Android 无障碍服务检测通知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12554448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android accessibility service detect notification
提问by ng93
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.
我试图让我的应用程序在显示通知时检测到。我已在设置应用程序中启用它并onServiceConnected
确实被调用,但是当我通过 gmail 应用程序创建通知或接收电子邮件时,onAccessibilityEvent
没有任何反应,不会被调用。
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
通知服务.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.
谢谢你的帮助。
回答by alanv
Accessibility services in Android 4.0 and above can behave strangely if there is no accessibility-servicemeta-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.
如果清单中没有定义无障碍服务元数据标签,Android 4.0 及更高版本中的无障碍服务可能会出现奇怪的行为。尝试按照下面的示例定义元数据。您应该继续使用 setServiceInfo() 来保持与 4.0 之前的设备的向后兼容性。
Also, I would recommend specifying a feedback type that is specific to your service, rather than using "all".
此外,我建议指定特定于您的服务的反馈类型,而不是使用“全部”。
AndroidManifest.xml
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
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
通知服务.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);
}
回答by anurag
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
使用 AccessibilityService 的应用程序需要从 settings>Accessibility 获得许可才能访问系统事件。允许设置权限。这可能有效
check this link
检查此链接