需求
在开发微动手势的时候,做了一个通知触发的功能,就是在收到某个预设的通知的时候,自动触发某个动作,因此需要监听通知消息。
方案
最开始想使用传统的NotificationListenerService,但是这个微动手势App恰好使用了安卓的无障碍服务, 也就是Accessibility Service, 翻这个文档的时候居然发现,无障碍服务里可以监听通知事件,下表是无障碍服务能提供的事件类型:
Event types
AccessibilityEvent#TYPE_VIEW_CLICKED
AccessibilityEvent#TYPE_VIEW_LONG_CLICKED
AccessibilityEvent#TYPE_VIEW_FOCUSED
AccessibilityEvent#TYPE_VIEW_SELECTED
AccessibilityEvent#TYPE_VIEW_TEXT_CHANGED
AccessibilityEvent#TYPE_WINDOW_STATE_CHANGED
AccessibilityEvent#TYPE_NOTIFICATION_STATE_CHANGED
AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_START
AccessibilityEvent#TYPE_TOUCH_EXPLORATION_GESTURE_END
AccessibilityEvent#TYPE_VIEW_HOVER_ENTER
AccessibilityEvent#TYPE_VIEW_HOVER_EXIT
AccessibilityEvent#TYPE_VIEW_SCROLLED
AccessibilityEvent#TYPE_VIEW_TEXT_SELECTION_CHANGED
AccessibilityEvent#TYPE_WINDOW_CONTENT_CHANGED
AccessibilityEvent#TYPE_ANNOUNCEMENT
AccessibilityEvent#TYPE_GESTURE_DETECTION_START
AccessibilityEvent#TYPE_GESTURE_DETECTION_END
AccessibilityEvent#TYPE_TOUCH_INTERACTION_START
AccessibilityEvent#TYPE_TOUCH_INTERACTION_END
AccessibilityEvent#TYPE_VIEW_ACCESSIBILITY_FOCUSED
AccessibilityEvent#TYPE_WINDOWS_CHANGED
AccessibilityEvent#TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED
类型好多啊,通知就是上图中的黑体的事件,这样省去了单独的NotificationListenerService和额外的权限了,对这个App来说自然比较好。
实现
找到了方案,实现并不复杂。
- 在AndroidManifest.xml注册无障碍服务
<service
android:name=".ServicePlus"
android:label="@string/service_plus"
android:stopWithTask="false"
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/service_plus" />
</service>
上面的android:stopWithTask是个有意思的标志,按照文档的描述,应该是指这个服务不会随前台退出而退出,参考下面的一段话:
FLAG_STOP_WITH_TASK Added in API level 14public static final int FLAG_STOP_WITH_TASK
Bit in flags: If set, the service will automatically be stopped by the system if the user removes a task that is rooted in one of the application's activities. Set from the R.attr.stopWithTask attribute.Constant Value: 1 (0x00000001)
但在国内安卓环境下,我看应该是不起作用的。 ;)
- 使用xml对无障碍服务配置参数,就是上面的android:resource指向的xml:
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:accessibilityEventTypes="typeNotificationStateChanged"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="100"
android:accessibilityFlags="flagRequestFilterKeyEvents"
android:description="@string/service_plus_tip"
tools:targetApi="n" />
其中的accessibilityEventTypes就指定了通知类型的事件。
- 可以接收事件了,看下相应的代码
public class ServiceBase extends AccessibilityService {
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
try {
if(event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
Notification notif = (Notification) event.getParcelableData();
if (notif != null) {
String title = notif.extras.getString(Notification.EXTRA_TITLE);
String text = notif.extras.getString(Notification.EXTRA_TEXT);
// ...
}
}
} catch(Exception e) {
Log.e(Consts.LOG, "event", e);
}
}
}
这个代码很好理解,不做什么解释了,Notification类还有很多获取通知相关信息的方法,可以参考Google文档。
总结
如果你开发的App用到了无障碍服务,监听通知就有一个更简单的方法。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。