我知道这个问题已被问过很多次果冻豆,但我不知道如何让它起作用。我正在尝试使用AccessibilityService获取Notification事件。这是我试过的代码......
public class NotificationService extends AccessibilityService {
Context context;
private boolean isInit;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
Log.d("NotificationService","oncreate of NotificationService");
}
@Override
public void onAccessibilityEvent(AccessibilityEvent sbn) {
Log.d("NotificationService","onAccessibilityEvent");
if (sbn.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
//Do something, eg getting packagename
final String packagename = String.valueOf(sbn.getPackageName());
String pack = String.valueOf(sbn.getPackageName());
/*String ticker = sbn.getNotification().tickerText.toString();
Bundle extras = sbn.getNotification().extras;
String title = extras.getString("android.title");*/
String text = sbn.getContentDescription().toString();
Log.d("NotificationService", "Package = "+pack);
Log.d("NotificationService", "Text = " + text);
/*Intent msgrcv = new Intent("Msg");
msgrcv.putExtra("package", pack);
msgrcv.putExtra("ticker", ticker);
msgrcv.putExtra("title", title);
msgrcv.putExtra("text", text);
LocalBroadcastManager.getInstance(context).sendBroadcast(msgrcv);*/
}
}
@Override
protected void onServiceConnected() {
if (isInit) {
return;
}
AccessibilityServiceInfo info = new AccessibilityServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
setServiceInfo(info);
isInit = true;
}
@Override
public void onInterrupt() {
isInit = false;
}
我的清单文件
<service android:name=".NotificationService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
<category android:name="android.accessibilityservice.category.FEEDBACK_SPOKEN" />
</intent-filter>
<meta-data android:name="android.accessibilityservice"
android:resource="@xml/accessibilityservice" />
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" />
请帮助我,我已经坚持了很长时间。