Accessibility Service详解
Posted Sun_TTTT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
@Override
protected void onServiceConnected()
super.onServiceConnected();
Log.e(TAG,"onServiceConnected");
@Override
public void onAccessibilityEvent(AccessibilityEvent event)
AccessibilityNodeInfo nodeInfo =event.getSource();
@Override
public 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();
@Override
protected 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详解的主要内容,如果未能解决你的问题,请参考以下文章
如何检查其他应用程序是不是在 Android 中使用辅助功能(BIND_ACCESSIBILITY_SERVICE)权限