我試圖讓我的應用在顯示通知時進行檢測。我已經在設置應用程序中啓用它,並且onServiceConnected
確實被調用,但是當我通過Gmail應用程序創建通知或接收電子郵件時,什麼也沒有發生,onAccessibilityEvent
不會被調用。Android無障礙服務檢測通知
Android清單:
<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");
}
}
感謝您的幫助。
您是否找到了解決辦法?我也有完全一樣的問題。 –
alanv的答案對我有用 – ng93
對於什麼系統版本你編程? ICS?果凍豆? –