Accessibility Service详解

2024-02-23 11:38

本文主要是介绍Accessibility Service详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Accessibility Service详解

按照开发文档的介绍,Accessibility service 是一个用来帮助残疾人使用Android设备和应用,它在后台接受一系列的system的回调,并且触发AccessibilityEvent。当比如焦点变化、一个按钮被按下时,就会标明这些event。

不过这个残疾人服务感觉被用歪了。因为他能够接受一些界面元素的状态,并且能够对这些元素添加动作。比如说 当一个界面显示时,我们可以获取其中的一个button,并且进行点击。在国内,大部分将之用来抢红包。当然也有其他的应用,比如说 免Root自动安装应用(很多应用市场提供这个功能)、应用的免卸载、浏览器的劫持、自动回复、清除后台程序等等。只有想不到没有做不到。

使用

一般使用Accessibility Service只需要三步。第一步,继承AccessibilityService。

public class CAccessibilityService extends AccessibilityService {private String TAG = CAccessibilityService.class.getSimpleName();@Overrideprotected void onServiceConnected() {super.onServiceConnected();Log.e(TAG,"onServiceConnected");}@Overridepublic void onAccessibilityEvent(AccessibilityEvent event) {AccessibilityNodeInfo nodeInfo =event.getSource();}@Overridepublic void onInterrupt() {}
}

第二步,创建配置文件(也可变成动态注册)。

<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"android:description="@string/accessibility_service_description" // 对这个AccessibilityService进行描述,用在设置->辅助功能界面android:packageNames="com.example.android.apis" //需要监听的应用包名,如果不设置,则监听所有程序android:accessibilityEventTypes="typeAllMask" //需要监听的事件类型,有窗口变化、点击、焦点变化等很多事件类型。如果想要监听所有的事件类型,则使用typeAllMask。如果需要多个事件类型可以使用“|”进行分隔。下面有支持的事件类型android:accessibilityFlags="flagDefault" //附加标志android:accessibilityFeedbackType="feedbackSpoken" //service的反馈类型android:notificationTimeout="100" //连续两个相同类型的事件之间的最小间隔android:canRetrieveWindowContent="true" //是否接受活动窗口的内容android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity" //允许修改这个service设置的activity
/>

事件类型
View clicked - represents the event of clicking on a View like Button, CompoundButton, etc.
Type:TYPE_VIEW_CLICKED
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source’s sub-tree.
isEnabled() - Whether the source is enabled.
isPassword() - Whether the source is password.
isChecked() - Whether the source is checked.
getContentDescription() - The content description of the source.
getScrollX() - The offset of the source left edge in pixels (without descendants of AdapterView).
getScrollY() - The offset of the source top edge in pixels (without descendants of AdapterView).
getFromIndex() - The zero based index of the first visible item of the source, inclusive (for descendants of AdapterView).
getToIndex() - The zero based index of the last visible item of the source, inclusive (for descendants of AdapterView).
getItemCount() - The total items of the source (for descendants of AdapterView).
View long clicked - represents the event of long clicking on a View like Button, CompoundButton, etc
Type:TYPE_VIEW_LONG_CLICKED
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source’s sub-tree.
isEnabled() - Whether the source is enabled.
isPassword() - Whether the source is password.
isChecked() - Whether the source is checked.
getContentDescription() - The content description of the source.
getScrollX() - The offset of the source left edge in pixels (without descendants of AdapterView).
getScrollY() - The offset of the source top edge in pixels (without descendants of AdapterView).
getFromIndex() - The zero based index of the first visible item of the source, inclusive (for descendants of AdapterView).
getToIndex() - The zero based index of the last visible item of the source, inclusive (for descendants of AdapterView).
getItemCount() - The total items of the source (for descendants of AdapterView).
View selected - represents the event of selecting an item usually in the context of an AdapterView.
Type: TYPE_VIEW_SELECTED
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source’s sub-tree.
isEnabled() - Whether the source is enabled.
isPassword() - Whether the source is password.
isChecked() - Whether the source is checked.
getItemCount() - The number of selectable items of the source.
getCurrentItemIndex() - The currently selected item index.
getContentDescription() - The content description of the source.
getScrollX() - The offset of the source left edge in pixels (without descendants of AdapterView).
getScrollY() - The offset of the source top edge in pixels (without descendants of AdapterView).
getFromIndex() - The zero based index of the first visible item of the source, inclusive (for descendants of AdapterView).
getToIndex() - The zero based index of the last visible item of the source, inclusive (for descendants of AdapterView).
getItemCount() - The total items of the source (for descendants of AdapterView).
View focused - represents the event of focusing a View.
Type: TYPE_VIEW_FOCUSED
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source’s sub-tree.
isEnabled() - Whether the source is enabled.
isPassword() - Whether the source is password.
isChecked() - Whether the source is checked.
getItemCount() - The number of focusable items on the screen.
getCurrentItemIndex() - The currently focused item index.
getContentDescription() - The content description of the source.
getScrollX() - The offset of the source left edge in pixels (without descendants of AdapterView).
getScrollY() - The offset of the source top edge in pixels (without descendants of AdapterView).
getFromIndex() - The zero based index of the first visible item of the source, inclusive (for descendants of AdapterView).
getToIndex() - The zero based index of the last visible item of the source, inclusive (for descendants of AdapterView).
getItemCount() - The total items of the source (for descendants of AdapterView).
View text changed - represents the event of changing the text of an EditText.
Type: TYPE_VIEW_TEXT_CHANGED
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source.
isEnabled() - Whether the source is enabled.
isPassword() - Whether the source is password.
isChecked() - Whether the source is checked.
getFromIndex() - The text change start index.
getAddedCount() - The number of added characters.
getRemovedCount() - The number of removed characters.
getBeforeText() - The text of the source before the change.
getContentDescription() - The content description of the source.
View text selection changed - represents the event of changing the text selection of an EditText.
Type: TYPE_VIEW_TEXT_SELECTION_CHANGED
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source.
isPassword() - Whether the source is password.
getFromIndex() - The selection start index.
getToIndex() - The selection end index.
getItemCount() - The length of the source text.
isEnabled() - Whether the source is enabled.
getContentDescription() - The content description of the source.
View text traversed at movement granularity - represents the event of traversing the text of a view at a given granularity. For example, moving to the next word.
Type: TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getMovementGranularity() - Sets the granularity at which a view’s text was traversed.
getText() - The text of the source’s sub-tree.
getFromIndex() - The start the text that was skipped over in this movement. This is the starting point when moving forward through the text, but not when moving back.
getToIndex() - The end of the text that was skipped over in this movement. This is the ending point when moving forward through the text, but not when moving back.
isPassword() - Whether the source is password.
isEnabled() - Whether the source is enabled.
getContentDescription() - The content description of the source.
getMovementGranularity() - Sets the granularity at which a view’s text was traversed.
getAction() - Gets traversal action which specifies the direction.
View scrolled - represents the event of scrolling a view. If the source is a descendant of AdapterView the scroll is reported in terms of visible items - the first visible item, the last visible item, and the total items - because the the source is unaware of its pixel size since its adapter is responsible for creating views. In all other cases the scroll is reported as the current scroll on the X and Y axis respectively plus the height of the source in pixels.
Type: TYPE_VIEW_SCROLLED
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source’s sub-tree.
isEnabled() - Whether the source is enabled.
getContentDescription() - The content description of the source.
getScrollX() - The offset of the source left edge in pixels (without descendants of AdapterView).
getScrollY() - The offset of the source top edge in pixels (without descendants of AdapterView).
getFromIndex() - The zero based index of the first visible item of the source, inclusive (for descendants of AdapterView).
getToIndex() - The zero based index of the last visible item of the source, inclusive (for descendants of AdapterView).
getItemCount() - The total items of the source (for descendants of AdapterView).
Note: This event type is not dispatched to descendants though View.dispatchPopulateAccessibilityEvent(AccessibilityEvent), hence the event source View and the sub-tree rooted at it will not receive calls to View.onPopulateAccessibilityEvent(AccessibilityEvent). The preferred way to add text content to such events is by setting the contentDescription of the source view.
TRANSITION TYPES
Window state changed - represents the event of opening a PopupWindow, Menu, Dialog, etc.
Type: TYPE_WINDOW_STATE_CHANGED
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source’s sub-tree.
isEnabled() - Whether the source is enabled.
Window content changed - represents the event of change in the content of a window. This change can be adding/removing view, changing a view size, etc.
Note: This event is fired only for the window source of the last accessibility event different from TYPE_NOTIFICATION_STATE_CHANGED and its purpose is to notify clients that the content of the user interaction window has changed.
Type: TYPE_WINDOW_CONTENT_CHANGED
Properties:
getEventType() - The type of the event.
getContentChangeTypes() - The type of content changes.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
Note: This event type is not dispatched to descendants though View.dispatchPopulateAccessibilityEvent(AccessibilityEvent), hence the event source View and the sub-tree rooted at it will not receive calls to View.onPopulateAccessibilityEvent(AccessibilityEvent). The preferred way to add text content to such events is by setting the contentDescription of the source view.
Windows changed - represents the event of changes in the windows shown on the screen such as a window appeared, a window disappeared, a window size changed, a window layer changed, etc.
Type: TYPE_WINDOWS_CHANGED
Properties:
getEventType() - The type of the event.
getEventTime() - The event time.
Note: You can retrieve the AccessibilityWindowInfo for the window source of the event via getSource() to get the source node on which then call AccessibilityNodeInfo.getWindow() to get the window. Also all windows on the screen can be retrieved by a call to android.accessibilityservice.AccessibilityService.getWindows().
NOTIFICATION TYPES
Notification state changed - represents the event showing a transient piece of information to the user. This information may be a Notification or Toast.
Type: TYPE_NOTIFICATION_STATE_CHANGED
Properties:
getEventType() - The type of the event.
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getParcelableData() - The posted Notification, if applicable.
getText() - Displayed text of the Toast, if applicable, or may contain text from the Notification, although getParcelableData() is a richer set of data for Notification.
EXPLORATION TYPES
View hover enter - represents the event of beginning to hover over a View. The hover may be generated via exploring the screen by touch or via a pointing device.
Type: TYPE_VIEW_HOVER_ENTER
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source’s sub-tree.
isEnabled() - Whether the source is enabled.
getContentDescription() - The content description of the source.
getScrollX() - The offset of the source left edge in pixels (without descendants of AdapterView).
getScrollY() - The offset of the source top edge in pixels (without descendants of AdapterView).
getFromIndex() - The zero based index of the first visible item of the source, inclusive (for descendants of AdapterView).
getToIndex() - The zero based index of the last visible item of the source, inclusive (for descendants of AdapterView).
getItemCount() - The total items of the source (for descendants of AdapterView).
View hover exit - represents the event of stopping to hover over a View. The hover may be generated via exploring the screen by touch or via a pointing device.
Type: TYPE_VIEW_HOVER_EXIT
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the source’s sub-tree.
isEnabled() - Whether the source is enabled.
getContentDescription() - The content description of the source.
getScrollX() - The offset of the source left edge in pixels (without descendants of AdapterView).
getScrollY() - The offset of the source top edge in pixels (without descendants of AdapterView).
getFromIndex() - The zero based index of the first visible item of the source, inclusive (for descendants of AdapterView).
getToIndex() - The zero based index of the last visible item of the source, inclusive (for descendants of AdapterView).
getItemCount() - The total items of the source (for descendants of AdapterView).
Touch interaction start - represents the event of starting a touch interaction, which is the user starts touching the screen.
Type: TYPE_TOUCH_INTERACTION_START
Properties:
getEventType() - The type of the event.
Note: This event is fired only by the system and is not passed to the view tree to be populated.
Touch interaction end - represents the event of ending a touch interaction, which is the user stops touching the screen.
Type: TYPE_TOUCH_INTERACTION_END
Properties:
getEventType() - The type of the event.
Note: This event is fired only by the system and is not passed to the view tree to be populated.
Touch exploration gesture start - represents the event of starting a touch exploring gesture.
Type: TYPE_TOUCH_EXPLORATION_GESTURE_START
Properties:
getEventType() - The type of the event.
Note: This event is fired only by the system and is not passed to the view tree to be populated.
Touch exploration gesture end - represents the event of ending a touch exploring gesture.
Type: TYPE_TOUCH_EXPLORATION_GESTURE_END
Properties:
getEventType() - The type of the event.
Note: This event is fired only by the system and is not passed to the view tree to be populated.
Touch gesture detection start - represents the event of starting a user gesture detection.
Type: TYPE_GESTURE_DETECTION_START
Properties:
getEventType() - The type of the event.
Note: This event is fired only by the system and is not passed to the view tree to be populated.
Touch gesture detection end - represents the event of ending a user gesture detection.
Type: TYPE_GESTURE_DETECTION_END
Properties:
getEventType() - The type of the event.
Note: This event is fired only by the system and is not passed to the view tree to be populated.
MISCELLANEOUS TYPES
Announcement - represents the event of an application making an announcement. Usually this announcement is related to some sort of a context change for which none of the events representing UI transitions is a good fit. For example, announcing a new page in a book.
Type: TYPE_ANNOUNCEMENT
Properties:
getEventType() - The type of the event.
getSource() - The source info (for registered clients).
getClassName() - The class name of the source.
getPackageName() - The package name of the source.
getEventTime() - The event time.
getText() - The text of the announcement.
isEnabled() - Whether the source is enabled.

如果是动态注册,需要在OnServiceConnected()中使用setServiceInfo();

 @Overrideprotected void onServiceConnected() {super.onServiceConnected();AccessibilityServiceInfo info = new AccessibilityServiceInfo();info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;...setServiceInfo(info);Log.e(TAG,"onServiceConnected");}

但是这种动态注册只能进行几种配置:eventTypes, feedbackType, flags, notificationTimeout, packageNames。
第三步,在AndroidManifest中进行注册。

<application><service android:name=".MyAccessibilityService"android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"android:label="@string/accessibility_service_label"><intent-filter><action android:name="android.accessibilityservice.AccessibilityService" /></intent-filter><meta-data
    android:name="android.accessibilityservice"android:resource="@xml/accessibility_service_config" /></service></application>

这个之后,当监听的事件发生时,便会源源不断的回调public void onAccessibilityEvent(AccessibilityEvent event)方法。你可以在这个方法中执行自己的逻辑。比如说点击、输入等等。

。。。。未完

这篇关于Accessibility Service详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/738506

相关文章

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

Redis 的 SUBSCRIBE命令详解

《Redis的SUBSCRIBE命令详解》Redis的SUBSCRIBE命令用于订阅一个或多个频道,以便接收发送到这些频道的消息,本文给大家介绍Redis的SUBSCRIBE命令,感兴趣的朋友跟随... 目录基本语法工作原理示例消息格式相关命令python 示例Redis 的 SUBSCRIBE 命令用于订

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python中 try / except / else / finally 异常处理方法详解

《Python中try/except/else/finally异常处理方法详解》:本文主要介绍Python中try/except/else/finally异常处理方法的相关资料,涵... 目录1. 基本结构2. 各部分的作用tryexceptelsefinally3. 执行流程总结4. 常见用法(1)多个e

SpringBoot日志级别与日志分组详解

《SpringBoot日志级别与日志分组详解》文章介绍了日志级别(ALL至OFF)及其作用,说明SpringBoot默认日志级别为INFO,可通过application.properties调整全局或... 目录日志级别1、级别内容2、调整日志级别调整默认日志级别调整指定类的日志级别项目开发过程中,利用日志

Java中的抽象类与abstract 关键字使用详解

《Java中的抽象类与abstract关键字使用详解》:本文主要介绍Java中的抽象类与abstract关键字使用详解,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、抽象类的概念二、使用 abstract2.1 修饰类 => 抽象类2.2 修饰方法 => 抽象方法,没有

MySQL8 密码强度评估与配置详解

《MySQL8密码强度评估与配置详解》MySQL8默认启用密码强度插件,实施MEDIUM策略(长度8、含数字/字母/特殊字符),支持动态调整与配置文件设置,推荐使用STRONG策略并定期更新密码以提... 目录一、mysql 8 密码强度评估机制1.核心插件:validate_password2.密码策略级

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

详解python pycharm与cmd中制表符不一样

《详解pythonpycharm与cmd中制表符不一样》本文主要介绍了pythonpycharm与cmd中制表符不一样,这个问题通常是因为PyCharm和命令行(CMD)使用的制表符(tab)的宽... 这个问题通常是因为PyCharm和命令行(CMD)使用的制表符(tab)的宽度不同导致的。在PyChar