安卓使用无障碍服务监听通知

2020/5/22 14:26:48

本文主要是介绍安卓使用无障碍服务监听通知,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

需求

在开发微动手势的时候,做了一个通知触发的功能,就是在收到某个预设的通知的时候,自动触发某个动作,因此需要监听通知消息。

方案

最开始想使用传统的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来说自然比较好。

实现

找到了方案,实现并不复杂。

  1. 在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 14

public 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)

但在国内安卓环境下,我看应该是不起作用的。 ;)

  1. 使用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就指定了通知类型的事件。

  1. 可以接收事件了,看下相应的代码
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用到了无障碍服务,监听通知就有一个更简单的方法。



这篇关于安卓使用无障碍服务监听通知的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程