更新记录
1.0.2(2026-01-07)
更新兼容HbuilderX 4.29
1.0.1(2026-01-06)
兼容更早HbuilderX版本
1.0.0(2026-01-06)
平台兼容性
uni-app
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | - | - | - | - | - | - | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|
| - | - | - | - | - | - | - | - | - | - | - |
其他
| 多语言 | 暗黑模式 | 宽屏模式 |
|---|---|---|
| √ | √ | √ |
Android 无障碍服务插件
插件概述
这是一个基于 Android 无障碍服务(AccessibilityService) 的功能强大的自动化操作插件。它允许应用程序模拟用户操作,实现自动化测试、辅助功能、界面自动化等多种场景。
核心特性
- 🔧 无障碍服务管理:检测、启用和管理无障碍服务状态
- 🎯 节点操作:查找、识别和操作界面元素
- 📡 事件监听:实时监听界面变化和用户交互事件
- 👆 手势模拟:支持点击、滑动、长按、拖拽等多种手势
- 🌐 全局操作:模拟系统级操作(返回、Home、最近任务等)
函数详细说明与使用方式
本文档详细介绍插件的各个功能模块和使用方法。
1. 无障碍服务管理函数
isAccessibilityEnabled()
功能: 检查无障碍服务是否已启用
返回值: boolean - 服务是否启用
使用场景: 在调用其他功能前检查服务状态
示例代码:
if (isAccessibilityEnabled()) {
console.log("无障碍服务已启用")
} else {
console.log("请先启用无障碍服务")
}
openAccessibilitySettings()
功能: 打开系统无障碍设置页面
返回值: boolean - 是否成功打开设置页面
使用场景: 引导用户开启无障碍服务
示例代码:
if (!isAccessibilityEnabled()) {
const success = openAccessibilitySettings()
console.log(success ? "已打开设置页面" : "打开设置失败")
}
## 2. 全局操作函数
### performBackAction()
**功能**: 模拟按下返回键
**返回值**: `boolean` - 操作是否成功
**示例代码**:
```javascript
// 返回上一页
const success = performBackAction()
console.log(success ? "返回成功" : "返回失败")
performHomeAction()
功能: 模拟按下Home键,返回主屏幕
返回值: boolean - 操作是否成功
示例代码:
// 返回桌面
performHomeAction()
performRecentAppsAction()
功能: 显示最近任务列表
返回值: boolean - 操作是否成功
示例代码:
// 切换应用
performRecentAppsAction()
performNotificationsAction()
功能: 下拉通知栏
返回值: boolean - 操作是否成功
示例代码:
// 查看通知
performNotificationsAction()
3. 事件监听函数
startListener(options, callback)
功能: 启动事件监听器
参数:
options: 配置对象packageNames:string[]- 需要监听的包名列表,为空时监听所有应用listenAllEvents:boolean- 是否监听所有事件,默认为false
callback:(data: string[]) => void- 事件回调函数,接收三个字符串的数组:[className, packageName, eventType]
示例代码:
// 监听指定应用的点击事件
const options = {
packageNames: ["com.tencent.mm", "com.tencent.mobileqq"],
listenAllEvents: false
}
startListener(options, (eventData) => {
const [className, packageName, eventType] = eventData
console.log(`事件:包名=${packageName}, 类名=${className}, 类型=${eventType}`)
})
stopListener()
功能: 停止事件监听器
示例代码:
// 停止监听
stopListener()
4. 节点查找与识别函数
checkNodeByViewIdAndTextAndContentDesc(options)
功能: 检查满足多个条件的节点是否存在
参数:
options:UTSJSONObject | null- 节点查找参数viewId:string | null- 视图ID,如:"com.example:id/button_ok"text:string | null- 节点文本内容(支持部分匹配,不区分大小写)contentDesc:string | null- 节点内容描述(支持部分匹配,不区分大小写)
返回值: boolean - 是否存在满足条件的节点
示例代码:
// 检查微信登录按钮是否存在
const exists = checkNodeByViewIdAndTextAndContentDesc({
text: "登录"
})
console.log(exists ? "登录按钮存在" : "登录按钮不存在")
// 通过视图ID和文本检查
const buttonExists = checkNodeByViewIdAndTextAndContentDesc({
viewId: "com.tencent.mm:id/b_2",
text: "确定"
})
checkMultipleNodeByViewIdsAndTextsAndContentDescs(conditions)
功能: 批量检查多个条件是否都满足
参数:
conditions:UTSJSONObject[]- 条件数组,每个条件格式同 checkNodeByViewIdAndTextAndContentDesc
返回值: boolean - 是否所有条件都满足
示例代码:
// 检查多个元素是否都存在
const allExist = checkMultipleNodeByViewIdsAndTextsAndContentDescs([
{ text: "用户名" },
{ text: "密码" },
{ text: "登录" }
])
console.log(allExist ? "所有元素都存在" : "部分元素缺失")
getNodesCount(options)
功能: 获取满足条件的节点数量
参数:
options:UTSJSONObject | null- 节点查找参数className:string | null- 节点类名packageName:string | null- 应用包名text:string | null- 节点文本内容contentDesc:string | null- 节点内容描述viewId:string | null- 视图IDhint:string | null- 提示文本visible:boolean | null- 是否可见enabled:boolean | null- 是否启用clickable:boolean | null- 是否可点击focusable:boolean | null- 是否可获取焦点focused:boolean | null- 是否已获取焦点
返回值: number - 满足条件的节点数量
示例代码:
// 获取页面中所有"确定"按钮的数量
const count = getNodesCount({ text: "确定" })
console.log(`找到 ${count} 个确定按钮`)
// 获取所有可见的文本节点
const visibleTextCount = getNodesCount({
text: null,
visible: true
})
findNodes(viewId?, text?, contentDesc?)
功能: 查找所有满足条件的节点并返回详细信息
返回值: Array<UTSJSONObject> - 节点信息数组,每个节点包含:
index: 节点索引className: 节点类名text: 节点文本contentDescription: 内容描述viewId: 视图IDvisible: 是否可见enabled: 是否启用clickable: 是否可点击bounds: 边界信息(left, top, right, bottom, width, height)
示例代码:
const nodes = findNodes(null, "登录", null)
nodes.forEach((node, index) => {
console.log(`节点${index}: ${node.text}`)
console.log(`位置: ${JSON.stringify(node.bounds)}`)
console.log(`可见: ${node.visible}, 可用: ${node.enabled}`)
})
## 5. 节点操作函数
### operateNodeAdvanced(options)
**功能**: 对满足条件的节点执行各种操作(点击、输入文本、滚动等)
**参数**:
- `options`: `UTSJSONObject | null` - 节点操作参数
- `index`: `number[]` - 节点索引数组(从0开始)
- `className`: `string | null` - 节点类名
- `packageName`: `string | null` - 应用包名
- `text`: `string | null` - 节点文本内容
- `contentDesc`: `string | null` - 节点内容描述
- `viewId`: `string | null` - 视图ID
- `hint`: `string | null` - 提示文本
- `visible`: `boolean | null` - 是否可见
- `enabled`: `boolean | null` - 是否启用
- `clickable`: `boolean | null` - 是否可点击
- `focusable`: `boolean | null` - 是否可获取焦点
- `focused`: `boolean | null` - 是否已获取焦点
- `actionType`: `string` - 操作类型,支持:
- `"click"`: 点击
- `"doubleclick"`: 双击
- `"longclick"`: 长按
- `"focus"`: 获取焦点
- `"select"`: 选择
- `"scrollforward"`: 向前滚动
- `"scrollbackward"`: 向后滚动
- `edit`: `string | null` - 要输入的文本内容
**返回值**: `boolean` - 操作是否成功
**示例代码**:
```javascript
// 点击第一个"确定"按钮
operateNodeAdvanced({
text: "确定",
index: [0],
actionType: "click"
})
// 向输入框输入文本
operateNodeAdvanced({
viewId: "com.example:id/input_et",
index: [0],
actionType: "click",
edit: "Hello World"
})
// 长按第二个列表项
operateNodeAdvanced({
index: [1],
actionType: "longclick"
})
clickNodeIndex(options)
功能: 单击指定节点(快捷方法)
参数:
options:UTSJSONObject | null- 节点查找参数viewId:string | null- 节点视图IDtext:string | null- 节点文本内容contentDesc:string | null- 节点内容描述index:number- 节点索引(从0开始,默认为0)
返回值: boolean - 是否成功执行单击操作
示例代码:
// 点击搜索按钮
clickNodeIndex({ viewId: "com.example:id/search_button" })
// 点击第一个"下一步"文本按钮
clickNodeIndex({ text: "下一步" })
doubleClickNodeIndex(options)
功能: 双击指定节点
参数:
options:UTSJSONObject | null- 节点查找参数viewId:string | null- 节点视图IDtext:string | null- 节点文本内容contentDesc:string | null- 节点内容描述index:number- 节点索引(从0开始,默认为0)
返回值: boolean - 是否成功执行双击操作
示例代码:
// 双击图片放大
doubleClickNodeIndex({ contentDesc: "用户头像" })
longClickNodeIndex(options)
功能: 长按指定节点
参数:
options:UTSJSONObject | null- 节点查找参数viewId:string | null- 节点视图IDtext:string | null- 节点文本内容contentDesc:string | null- 节点内容描述index:number- 节点索引(从0开始,默认为0)
返回值: boolean - 是否成功执行长按操作
示例代码:
// 长按消息项显示菜单
longClickNodeIndex({ contentDesc: "消息内容" })
focusNodeIndex(options)
功能: 获取节点焦点
参数:
options:UTSJSONObject | null- 节点查找参数viewId:string | null- 节点视图IDhint:string | null- 提示文本text:string | null- 节点文本内容contentDesc:string | null- 节点内容描述index:number- 节点索引(从0开始,默认为0)
返回值: boolean - 是否成功获取焦点
示例代码:
// 获取输入框焦点
focusNodeIndex({ hint: "请输入用户名" })
inputTextToNode(options)
功能: 向指定节点输入文本
参数:
options:UTSJSONObject | null- 节点查找和输入参数viewId:string | null- 节点视图IDhint:string | null- 提示文本text:string | null- 节点文本内容contentDesc:string | null- 节点内容描述index:number- 节点索引(从0开始,默认为0)inputText:string- 要输入的文本内容
返回值: boolean - 是否成功输入文本
示例代码:
// 向搜索框输入文本
inputTextToNode({
viewId: "com.tencent.mm:id/search_et",
inputText: "Hello World"
})
// 向聊天输入框输入消息
inputTextToNode({
contentDesc: "输入消息",
inputText: "今天天气不错"
})
clearNodeText(options)
功能: 清除节点文本
参数:
options:UTSJSONObject | null- 节点查找参数viewId:string | null- 节点视图IDtext:string | null- 节点文本内容contentDesc:string | null- 节点内容描述index:number- 节点索引(从0开始,默认为0)
返回值: boolean - 是否成功清除文本
示例代码:
// 清空搜索框
clearNodeText({ viewId: "com.tencent.mm:id/search_et" })
scrollNode(options)
功能: 滚动节点
参数:
options:UTSJSONObject | null- 节点滚动参数viewId:string | null- 节点视图IDtext:string | null- 节点文本内容contentDesc:string | null- 节点内容描述index:number- 节点索引(从0开始,默认为0)direction:string- 滚动方向("forward"/"down" 或 "backward"/"up")
返回值: boolean - 是否成功滚动
示例代码:
// 向下滚动列表
scrollNode({
contentDesc: "消息列表",
direction: "down"
})
// 向上滚动页面
scrollNode({
index: 0,
direction: "up"
})
isNodeActionable(options, actionType)
功能: 检查节点是否支持指定操作
参数:
options:UTSJSONObject | null- 节点查找参数viewId:string | null- 节点视图IDtext:string | null- 节点文本内容contentDesc:string | null- 节点内容描述index:number- 节点索引(从0开始,默认为0)
actionType:string- 操作类型(如 "click", "focus" 等)
返回值: boolean - 节点是否支持指定操作
示例代码:
// 检查按钮是否可点击
const canClick = isNodeActionable({ text: "提交" }, "click")
console.log(canClick ? "按钮可点击" : "按钮不可点击")
6. 手势模拟函数
performClickAt(x, y, duration?)
功能: 在指定坐标执行点击操作
| 参数 | 类型 | 说明 |
|---|---|---|
x |
number |
X坐标(像素) |
y |
number |
Y坐标(像素) |
duration |
number |
点击持续时间(毫秒,默认100ms) |
注意: 需要 Android 7.0 (API 24) 或更高版本
示例代码:
// 点击屏幕中心
const screenSize = getScreenSize()
const centerX = screenSize.width / 2
const centerY = screenSize.height / 2
performClickAt(centerX, centerY)
// 点击指定按钮位置
performClickAt(500, 1000, 150) // 持续150ms的点击
performLongClickAt(x, y, duration?)
功能: 在指定坐标执行长按操作
| 参数 | 类型 | 说明 |
|---|---|---|
x |
number |
X坐标(像素) |
y |
number |
Y坐标(像素) |
duration |
number |
长按持续时间(毫秒,默认800ms) |
示例代码:
// 长按屏幕特定位置
performLongClickAt(300, 500, 1000) // 长按1秒
performDoubleClickAt(x, y, interval?)
功能: 在指定坐标执行双击操作
| 参数 | 类型 | 说明 |
|---|---|---|
x |
number |
X坐标(像素) |
y |
number |
Y坐标(像素) |
interval |
number |
两次点击间隔(毫秒,默认150ms) |
示例代码:
// 双击屏幕位置
performDoubleClickAt(400, 600, 200)
performClickCenter()
功能: 点击屏幕中心
示例代码:
// 点击屏幕中心
performClickCenter()
performClickAtRelative(relativeX, relativeY)
功能: 点击相对位置(0.0-1.0)
参数:
relativeX:number- 相对X位置(0.0-1.0)relativeY:number- 相对Y位置(0.0-1.0)
示例代码:
// 点击屏幕左上角1/4位置
performClickAtRelative(0.25, 0.25)
// 点击屏幕右下角
performClickAtRelative(0.8, 0.8)
7. 滑动操作函数
performSwipe(startX, startY, endX, endY, duration?)
功能: 从起点滑动到终点
参数:
startX, startY:number- 起点坐标endX, endY:number- 终点坐标duration:number- 滑动持续时间(毫秒,默认500ms)
示例代码:
// 从屏幕底部向上滑动
performSwipe(500, 1800, 500, 800, 300)
// 从右向左滑动
performSwipe(800, 1000, 200, 1000, 400)
performVerticalSwipe(x, startY, endY, duration?)
功能: 垂直滑动(X坐标不变)
示例代码:
// 垂直向下滑动
performVerticalSwipe(540, 300, 1500, 500)
// 垂直向上滑动
performVerticalSwipe(540, 1500, 300, 500)
performHorizontalSwipe(startX, endX, y, duration?)
功能: 水平滑动(Y坐标不变)
参数:
startX, endX:number- X轴起始和结束位置y:number- Y坐标(固定)duration:number- 滑动持续时间(毫秒,默认500ms)
示例代码:
// 水平向右滑动
performHorizontalSwipe(100, 800, 960, 400)
// 水平向左滑动
performHorizontalSwipe(800, 100, 960, 400)
performSwipeDown(x?, distance?, duration?)
功能: 模拟下滑(下拉刷新)
参数:
x:number- X坐标,默认500distance:number- 滑动距离,默认800像素duration:number- 滑动持续时间,默认500ms
示例代码:
// 下拉刷新
performSwipeDown()
// 在指定位置下拉更长的距离
performSwipeDown(300, 1000, 600)
performSwipeUp(x?, distance?, duration?)
功能: 模拟上滑(上拉加载更多)
示例代码:
// 上拉加载更多
performSwipeUp()
// 在右侧上拉
performSwipeUp(800, 1000, 600)
performDrag(startX, startY, endX, endY, pressDuration?, swipeDuration?)
功能: 模拟拖动操作(点击+滑动)
参数:
pressDuration:number- 按下持续时间(毫秒,默认200ms)swipeDuration:number- 滑动持续时间(毫秒,默认300ms)
示例代码:
// 拖动元素
performDrag(200, 500, 600, 500, 300, 400)
// 快速拖动
performDrag(100, 1000, 100, 300, 100, 200)
8. 信息获取函数
getCurrentActivityInfo()
功能: 获取当前Activity信息
返回值: Map<string, string> | null - 包含packageName和className
示例代码:
const activityInfo = getCurrentActivityInfo()
if (activityInfo) {
console.log(`当前应用: ${activityInfo.packageName}`)
console.log(`当前页面: ${activityInfo.className}`)
}
getScreenSize()
功能: 获取屏幕尺寸
返回值: Map<string, number> - 包含width和height
示例代码:
const screenSize = getScreenSize()
console.log(`屏幕尺寸: ${screenSize.width} × ${screenSize.height}`)
// 计算屏幕中心
const centerX = screenSize.width / 2
const centerY = screenSize.height / 2
launchApp(packageName, activityPage)
功能: 启动指定应用
参数:
packageName:String- 应用包名activityPage:String- Activity页面类名(可选)
返回值: boolean - 是否成功启动应用
示例代码:
// 启动微信
launchApp("com.tencent.mm", "")
// 启动应用指定页面
launchApp("com.example.app", "com.example.app.MainActivity")
launchAppByURI(androidScheme)
功能: 通过URI Scheme启动应用
参数:
androidScheme:String- Android URI Scheme
返回值: boolean - 是否成功启动应用
示例代码:
// 通过Scheme启动应用
launchAppByURI("weixin://")
launchAppByURI("alipay://")
完整使用示例
示例1:自动化登录流程
// 检查无障碍服务
if (!isAccessibilityEnabled()) {
console.log("请先开启无障碍服务")
openAccessibilitySettings()
return
}
// 启动事件监听
startListener({packageNames: ["com.example.app"]}, (eventData) => {
console.log(`事件: ${eventData}`)
})
// 等待应用启动
setTimeout(() => {
// 输入用户名
inputTextToNode(
"com.example.app:id/username_et",
null,
"用户名",
0,
"testuser"
)
// 输入密码
inputTextToNode(
"com.example.app:id/password_et",
null,
"密码",
0,
"password123"
)
// 点击登录按钮
clickNodeIndex("com.example.app:id/login_btn", "登录", null, 0)
// 等待登录成功
setTimeout(() => {
// 检查登录是否成功
const success = checkNodeByViewIdAndTextAndContentDesc(
null,
"欢迎",
null
)
console.log(success ? "登录成功" : "登录失败")
// 停止监听
stopListener()
}, 3000)
}, 2000)
示例2:自动化滑动浏览
// 滑动浏览内容
function scrollToBottom() {
const screenSize = getScreenSize()
// 滑动10次
for (let i = 0; i < 10; i++) {
// 从底部向上滑动
performSwipe(
screenSize.width / 2,
screenSize.height - 200,
screenSize.width / 2,
200,
400
)
// 等待加载
setTimeout(() => {
// 检查是否需要继续滑动
const hasMore = checkNodeByViewIdAndTextAndContentDesc(
null,
"加载更多",
null
)
if (!hasMore) {
console.log("已浏览到底部")
return
}
}, 1000)
}
}
// 执行滑动
scrollToBottom()
示例3:界面元素监控
// 监控特定按钮状态
function monitorButton() {
setInterval(() => {
const buttonExists = checkNodeByViewIdAndTextAndContentDesc(
"com.example:id/submit_btn",
"提交",
null
)
const buttonEnabled = isNodeActionable(
"com.example:id/submit_btn",
"提交",
null,
0,
"click"
)
console.log(`按钮存在: ${buttonExists}, 可点击: ${buttonEnabled}`)
if (buttonExists && buttonEnabled) {
clickNodeIndex("com.example:id/submit_btn", "提交", null, 0)
console.log("已点击提交按钮")
}
}, 1000)
}
// 开始监控
monitorButton()
注意事项
1. 权限要求
- 需要在AndroidManifest.xml中声明无障碍服务权限
- 用户需要手动在系统设置中启用无障碍服务
- 需要Android 7.0或更高版本支持部分高级手势功能
2. 性能建议
- 避免过于频繁的节点查找操作
- 使用视图ID查找比文本查找效率更高
- 适当添加延迟等待界面加载
3. 错误处理
try {
const result = performClickAt(100, 200)
if (!result) {
console.log("点击操作失败")
}
} catch (error) {
console.error("操作异常:", error)
}
4. 最佳实践
- 先检查后操作:操作前先检查元素是否存在
- 使用索引:当有多个相同元素时,明确指定索引
- 添加等待:界面操作后适当添加延迟
- 异常处理:所有操作都应该有错误处理
- 资源释放:使用完成后停止监听
常见问题解答
Q1: 为什么无法找到元素?
- 检查无障碍服务是否已启用
- 确认应用包名和元素信息是否正确
- 元素可能被遮挡或未加载完成
Q2: 点击操作无效怎么办?
- 检查元素是否可见和启用
- 尝试使用坐标点击代替元素点击
- 确认没有其他弹窗遮挡
Q3: 如何获取元素的视图ID?
- 使用Android Studio的Layout Inspector
- 使用UIAutomator Viewer工具
- 查看应用的布局文件
Q4: 为什么手势操作在低版本Android上失败?
- 手势操作需要Android 7.0+支持
- 低版本Android可以使用传统点击方法 功能:从起点滑动到终点 参数:
startX, startY: number - 起点坐标
endX, endY: number - 终点坐标
duration: number - 滑动持续时间(毫秒,默认500ms)
示例代码:
javascript // 从屏幕底部向上滑动 performSwipe(500, 1800, 500, 800, 300)
// 从右向左滑动 performSwipe(800, 1000, 200, 1000, 400) performVerticalSwipe(x, startY, endY, duration?) 功能:垂直滑动(X坐标不变) 示例代码:
javascript // 垂直向下滑动 performVerticalSwipe(540, 300, 1500, 500)
// 垂直向上滑动 performVerticalSwipe(540, 1500, 300, 500) performHorizontalSwipe(startX, endX, y, duration?) 功能:水平滑动(Y坐标不变) 示例代码:
javascript // 水平向右滑动 performHorizontalSwipe(100, 800, 960, 400)
// 水平向左滑动 performHorizontalSwipe(800, 100, 960, 400) performSwipeDown(x?, distance?, duration?) 功能:模拟下滑(下拉刷新) 参数:
x: number - X坐标,默认500
distance: number - 滑动距离,默认800像素
duration: number - 滑动持续时间,默认500ms
示例代码:
javascript // 下拉刷新 performSwipeDown()
// 在指定位置下拉更长的距离 performSwipeDown(300, 1000, 600) performSwipeUp(x?, distance?, duration?) 功能:模拟上滑(上拉加载更多) 示例代码:
javascript // 上拉加载更多 performSwipeUp()
// 在右侧上拉 performSwipeUp(800, 1000, 600) performDrag(startX, startY, endX, endY, pressDuration?, swipeDuration?) 功能:模拟拖动操作(点击+滑动) 参数:
pressDuration: number - 按下持续时间(毫秒,默认200ms)
swipeDuration: number - 滑动持续时间(毫秒,默认300ms)
示例代码:
javascript // 拖动元素 performDrag(200, 500, 600, 500, 300, 400)
// 快速拖动 performDrag(100, 1000, 100, 300, 100, 200)
- 信息获取函数 getCurrentActivityInfo() 功能:获取当前Activity信息 返回值:Map<string, string> | null - 包含packageName和className
示例代码:
javascript
const activityInfo = getCurrentActivityInfo()
if (activityInfo) {
console.log(当前应用: ${activityInfo.packageName})
console.log(当前页面: ${activityInfo.className})
}
getScreenSize()
功能:获取屏幕尺寸
返回值:Map<string, number> - 包含width和height
示例代码:
javascript
const screenSize = getScreenSize()
console.log(屏幕尺寸: ${screenSize.width} × ${screenSize.height})
// 计算屏幕中心 const centerX = screenSize.width / 2 const centerY = screenSize.height / 2 完整使用示例 示例1:自动化登录流程 javascript // 检查无障碍服务 if (!isAccessibilityEnabled()) { console.log("请先开启无障碍服务") openAccessibilitySettings() return }
// 启动事件监听
startListener({packageNames: ["com.example.app"]}, (eventData) => {
console.log(事件: ${eventData})
})
// 等待应用启动 setTimeout(() => { // 输入用户名 inputTextToNode( "com.example.app:id/username_et", null, "用户名", 0, "testuser" )
// 输入密码
inputTextToNode(
"com.example.app:id/password_et",
null,
"密码",
0,
"password123"
)
// 点击登录按钮
clickNodeIndex("com.example.app:id/login_btn", "登录", null, 0)
// 等待登录成功
setTimeout(() => {
// 检查登录是否成功
const success = checkNodeByViewIdAndTextAndContentDesc(
null,
"欢迎",
null
)
console.log(success ? "登录成功" : "登录失败")
// 停止监听
stopListener()
}, 3000)
}, 2000) 示例2:自动化滑动浏览 javascript // 滑动浏览内容 function scrollToBottom() { const screenSize = getScreenSize()
// 滑动10次
for (let i = 0; i < 10; i++) {
// 从底部向上滑动
performSwipe(
screenSize.width / 2,
screenSize.height - 200,
screenSize.width / 2,
200,
400
)
// 等待加载
setTimeout(() => {
// 检查是否需要继续滑动
const hasMore = checkNodeByViewIdAndTextAndContentDesc(
null,
"加载更多",
null
)
if (!hasMore) {
console.log("已浏览到底部")
return
}
}, 1000)
}
}
// 执行滑动 scrollToBottom() 示例3:界面元素监控 javascript // 监控特定按钮状态 function monitorButton() { setInterval(() => { const buttonExists = checkNodeByViewIdAndTextAndContentDesc( "com.example:id/submit_btn", "提交", null )
const buttonEnabled = isNodeActionable(
"com.example:id/submit_btn",
"提交",
null,
0,
"click"
)
console.log(`按钮存在: ${buttonExists}, 可点击: ${buttonEnabled}`)
if (buttonExists && buttonEnabled) {
clickNodeIndex("com.example:id/submit_btn", "提交", null, 0)
console.log("已点击提交按钮")
}
}, 1000)
}
// 开始监控 monitorButton() 注意事项
- 权限要求 需要在AndroidManifest.xml中声明无障碍服务权限
用户需要手动在系统设置中启用无障碍服务
需要Android 7.0或更高版本支持部分高级手势功能
- 性能建议 避免过于频繁的节点查找操作
使用视图ID查找比文本查找效率更高
适当添加延迟等待界面加载
- 错误处理 javascript try { const result = performClickAt(100, 200) if (!result) { console.log("点击操作失败") } } catch (error) { console.error("操作异常:", error) }
- 最佳实践 先检查后操作:操作前先检查元素是否存在
使用索引:当有多个相同元素时,明确指定索引
添加等待:界面操作后适当添加延迟
异常处理:所有操作都应该有错误处理
资源释放:使用完成后停止监听
常见问题解答 Q1: 为什么无法找到元素? 检查无障碍服务是否已启用
确认应用包名和元素信息是否正确
元素可能被遮挡或未加载完成
Q2: 点击操作无效怎么办? 检查元素是否可见和启用
尝试使用坐标点击代替元素点击
确认没有其他弹窗遮挡
Q3: 如何获取元素的视图ID? 使用Android Studio的Layout Inspector
使用UIAutomator Viewer工具
查看应用的布局文件
Q4: 为什么手势操作在低版本Android上失败? 手势操作需要Android 7.0+支持
低版本Android可以使用传统点击方法

收藏人数:
购买普通授权版(
试用
使用 HBuilderX 导入示例项目
赞赏(0)
下载 7
赞赏 0
下载 13063638
赞赏 1841
赞赏
京公网安备:11010802035340号