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

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Java中的数组与集合基本用法详解

《Java中的数组与集合基本用法详解》本文介绍了Java数组和集合框架的基础知识,数组部分涵盖了一维、二维及多维数组的声明、初始化、访问与遍历方法,以及Arrays类的常用操作,对Java数组与集合相... 目录一、Java数组基础1.1 数组结构概述1.2 一维数组1.2.1 声明与初始化1.2.2 访问

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected