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

相关文章

MySQL中的交叉连接、自然连接和内连接查询详解

《MySQL中的交叉连接、自然连接和内连接查询详解》:本文主要介绍MySQL中的交叉连接、自然连接和内连接查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、引入二、交php叉连接(cross join)三、自然连接(naturalandroid join)四

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

mysql的基础语句和外键查询及其语句详解(推荐)

《mysql的基础语句和外键查询及其语句详解(推荐)》:本文主要介绍mysql的基础语句和外键查询及其语句详解(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录一、mysql 基础语句1. 数据库操作 创建数据库2. 表操作 创建表3. CRUD 操作二、外键

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

C语言函数递归实际应用举例详解

《C语言函数递归实际应用举例详解》程序调用自身的编程技巧称为递归,递归做为一种算法在程序设计语言中广泛应用,:本文主要介绍C语言函数递归实际应用举例的相关资料,文中通过代码介绍的非常详细,需要的朋... 目录前言一、递归的概念与思想二、递归的限制条件 三、递归的实际应用举例(一)求 n 的阶乘(二)顺序打印

Python Faker库基本用法详解

《PythonFaker库基本用法详解》Faker是一个非常强大的库,适用于生成各种类型的伪随机数据,可以帮助开发者在测试、数据生成、或其他需要随机数据的场景中提高效率,本文给大家介绍PythonF... 目录安装基本用法主要功能示例代码语言和地区生成多条假数据自定义字段小结Faker 是一个 python

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核