欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

无障碍

程序员文章站 2022-04-02 23:05:21
无障碍可以监听的事件类型常规用法节点的基本属性可以模拟的ACTION类型可执行的全局操作模拟执行手势可以监听的事件类型typeAllMask:接收所有事件。-------窗口事件相关(常用)---------typeWindowStateChanged:监听窗口状态变化,比如打开一个popupWindow,dialog,Activity切换等等。typeWindowContentChanged:监听窗口内容改变,比如根布局子view的变化。typeWindowsChanged:监听屏幕上显示的...

基本配置

继承AccessibilityService,编写自己的无障碍服务

public class MagicalService extends AccessibilityService {

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        //服务启动成功
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        //事件回调
    }

    @Override
    public void onInterrupt() {
        //服务中断
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

AndroidManifest中注册

<!-- 无障碍 -->
<service
    android:name=".service.MagicalService"
    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/magical" />

</service>

无障碍配置

<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask" 想要监听的事件类型
    android:accessibilityFeedbackType="feedbackGeneric"  反馈类型
    android:canPerformGestures="true"  是否可以执行手势
    android:canRetrieveWindowContent="true" 允许窗口检索内容
    android:description="@string/app_name" 无障碍服务描述,用户开启的时候可以看到
    android:notificationTimeout="10"> 相同事件的最小访问间隔

</accessibility-service>

可以监听的事件类型

typeAllMask:接收所有事件。
-------窗口事件相关(常用)---------
typeWindowStateChanged:监听窗口状态变化,比如打开一个popupWindow,dialog,Activity切换等等。
typeWindowContentChanged:监听窗口内容改变,比如根布局子view的变化。
typeWindowsChanged:监听屏幕上显示的系统窗口中的事件更改。 此事件类型只应由系统分派。
typeNotificationStateChanged:监听通知变化,比如notifacation和toast。
-----------View事件相关--------------
typeViewClicked:监听view点击事件。
typeViewLongClicked:监听view长按事件。
typeViewFocused:监听view焦点事件。
typeViewSelected:监听AdapterView中的上下文选择事件。
typeViewTextChanged:监听EditText的文本改变事件。
typeViewHoverEnter、typeViewHoverExit:监听view的视图悬停进入和退出事件。
typeViewScrolled:监听view滚动,此类事件通常不直接发送。
typeViewTextSelectionChanged:监听EditText选择改变事件。
typeViewAccessibilityFocused:监听view获得可访问性焦点事件。
typeViewAccessibilityFocusCleared:监听view清除可访问性焦点事件。
------------手势事件相关---------------
typeGestureDetectionStart、typeGestureDetectionEnd:监听手势开始和结束事件。
typeTouchInteractionStart、typeTouchInteractionEnd:监听用户触摸屏幕事件的开始和结束。
typeTouchExplorationGestureStart、typeTouchExplorationGestureEnd:监听触摸探索手势的开始和结束。

常规用法

//获取当前所在类
event.getClassName()
//根据显示内容来获取节点
getRootInActiveWindow().findAccessibilityNodeInfosByText("")
//根据控件ID来获取对应节点 API18以上
getRootInActiveWindow().findAccessibilityNodeInfosByViewId("android:id/list")

//然后对获取到的响应节点做一些操作 例如点击、改变状态、设置内容等。
//以及通过可以通过监听通知栏变化截取短信验证码自动填充等操作。

节点的基本属性

builder.append("; boundsInParent: ").append(mBoundsInParent);
builder.append("; boundsInScreen: ").append(mBoundsInScreen);

builder.append("; packageName: ").append(mPackageName);
builder.append("; className: ").append(mClassName);
builder.append("; text: ").append(mText);
builder.append("; error: ").append(mError);
builder.append("; maxTextLength: ").append(mMaxTextLength);
builder.append("; stateDescription: ").append(mStateDescription);
builder.append("; contentDescription: ").append(mContentDescription);
builder.append("; tooltipText: ").append(mTooltipText);
builder.append("; viewIdResName: ").append(mViewIdResourceName);

builder.append("; checkable: ").append(isCheckable());
builder.append("; checked: ").append(isChecked());
builder.append("; focusable: ").append(isFocusable());
builder.append("; focused: ").append(isFocused());
builder.append("; selected: ").append(isSelected());
builder.append("; clickable: ").append(isClickable());
builder.append("; longClickable: ").append(isLongClickable());
builder.append("; contextClickable: ").append(isContextClickable());
builder.append("; enabled: ").append(isEnabled());
builder.append("; password: ").append(isPassword());
builder.append("; scrollable: ").append(isScrollable());
builder.append("; importantForAccessibility: ").append(isImportantForAccessibility());
builder.append("; visible: ").append(isVisibleToUser());
builder.append("; actions: ").append(mActions);

可以模拟的ACTION类型

AccessibilityNodeInfo.ACTION_CLICK
AccessibilityNodeInfo.ACTION_LONG_CLICK
AccessibilityNodeInfo.ACTION_SET_TEXT
AccessibilityNodeInfo.ACTION_CUT
AccessibilityNodeInfo.ACTION_COPY
AccessibilityNodeInfo.ACTION_PASTE
AccessibilityNodeInfo.ACTION_SCROLL_FORWARD
AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD
        
AccessibilityNodeInfo.ACTION_FOCUS
AccessibilityNodeInfo.ACTION_CLEAR_FOCUS
AccessibilityNodeInfo.ACTION_SELECT
AccessibilityNodeInfo.ACTION_CLEAR_SELECTION
AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS
AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS
AccessibilityNodeInfo.ACTION_NEXT_AT_MOVEMENT_GRANULARITY
AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY
AccessibilityNodeInfo.ACTION_NEXT_HTML_ELEMENT
AccessibilityNodeInfo.ACTION_PREVIOUS_HTML_ELEMENT
AccessibilityNodeInfo.ACTION_SET_SELECTION
AccessibilityNodeInfo.ACTION_EXPAND
AccessibilityNodeInfo.ACTION_COLLAPSE
AccessibilityNodeInfo.ACTION_DISMISS

可执行的全局操作

/**
     * Action to go back.
     */
    public static final int GLOBAL_ACTION_BACK = 1;

    /**
     * Action to go home.
     */
    public static final int GLOBAL_ACTION_HOME = 2;

    /**
     * Action to toggle showing the overview of recent apps. Will fail on platforms that don't
     * show recent apps.
     */
    public static final int GLOBAL_ACTION_RECENTS = 3;

    /**
     * Action to open the notifications.
     */
    public static final int GLOBAL_ACTION_NOTIFICATIONS = 4;

    /**
     * Action to open the quick settings.
     */
    public static final int GLOBAL_ACTION_QUICK_SETTINGS = 5;

    /**
     * Action to open the power long-press dialog.
     */
    public static final int GLOBAL_ACTION_POWER_DIALOG = 6;

    /**
     * Action to toggle docking the current app's window
     */
    public static final int GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN = 7;

    /**
     * Action to lock the screen
     */
    public static final int GLOBAL_ACTION_LOCK_SCREEN = 8;

    /**
     * Action to take a screenshot
     */
    public static final int GLOBAL_ACTION_TAKE_SCREENSHOT = 9;

    /**
     * Action to send the KEYCODE_HEADSETHOOK KeyEvent, which is used to answer/hang up calls and
     * play/stop media
     * @hide
     */
    public static final int GLOBAL_ACTION_KEYCODE_HEADSETHOOK = 10;

    /**
     * Action to trigger the Accessibility Button
     * @hide
     */
    public static final int GLOBAL_ACTION_ACCESSIBILITY_BUTTON = 11;

    /**
     * Action to bring up the Accessibility Button's chooser menu
     * @hide
     */
    public static final int GLOBAL_ACTION_ACCESSIBILITY_BUTTON_CHOOSER = 12;

    /**
     * Action to trigger the Accessibility Shortcut. This shortcut has a hardware trigger and can
     * be activated by holding down the two volume keys.
     * @hide
     */
    public static final int GLOBAL_ACTION_ACCESSIBILITY_SHORTCUT = 13;

    /**
     * Action to show Launcher's all apps.
     * @hide
     */
    public static final int GLOBAL_ACTION_ACCESSIBILITY_ALL_APPS = 14;

模拟执行手势

Path path = new Path();
path.moveTo(300, 300);

//手势分发 最小版本24 7.0
dispatchGesture(new GestureDescription.Builder().addStroke(new GestureDescription.StrokeDescription(path, 100, 2000)).build(),
        new GestureResultCallback() {
            @Override
            public void onCompleted(GestureDescription gestureDescription) {
                super.onCompleted(gestureDescription);
            }

            @Override
            public void onCancelled(GestureDescription gestureDescription) {
                super.onCancelled(gestureDescription);
            }
        }, null);

    }

本文地址:https://blog.csdn.net/gitzzp/article/details/109356475