问题描述
我试图让每当显示一条通知我的应用程序检测。我在设置应用程序和 onServiceConnected
不会被调用,但是当我创建一个通知或通过Gmail应用程序没有任何反应收到一封电子邮件,<$ C使它$ C> onAccessibilityEvent 不会被调用。
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清单:
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
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.0及以上的可访问性服务可以运行异常,如果没有accessibility-service在清单中定义的元数据标记。尝试定义如在以下实施例中的元数据。你应该继续使用setServiceInfo(),以保持与pre-4.0器件的向后兼容性。
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" />
有你feedbackType错误。下面的纠正。不过,考虑使用更具体的反馈式。
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);
}
这篇关于Android辅助服务检测通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!