安装使用
ctrl shift p
Auto.js: Start Server
手机连接wifi
Auto.js Pro: 连接到新设备
全局
toast([string]) // 气泡提示框(在非autojs app上不显示)
sleep([毫秒]) // 延迟
exit() // 停止运行
setClip([string]) // 设置剪贴板内容
getClip([string]) // 获取剪贴板内容
log([string]) // 控制台打印
导入导出
// 导出
module.exports = {
func: () => {}
};
// 导入
const { func } = require([path]);
启动应用
app.launchApp([appName]) // 通过app名打开
app.launch([packageName]) // 通过包名打开
打开无障碍服务(需adb受权,重启手机不失效)
importClass(android.content.Context);
importClass(android.provider.Settings);
try {
var enabledServices = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
log('当前已启用的辅助服务\n', enabledServices);
var Services = enabledServices + ":org.autojs.autojspro/com.stardust.autojs.core.accessibility.AccessibilityService";
Settings.Secure.putString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, Services);
Settings.Secure.putString(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, '1');
toastLog("成功开启AutoJS的辅助服务");
} catch (error) {
//受权方法:开启usb调试并使用adb工具链接手机,执行 adb shell pm grant org.autojs.autojspro android.permission.WRITE_SECURE_SETTING
toastLog("\n请确保已给予 WRITE_SECURE_SETTINGS 权限\n\n受权代码已复制,请使用adb工具链接手机执行(重启不失效)\n\n", error);
setClip("adb shell pm grant org.autojs.autojspro android.permission.WRITE_SECURE_SETTINGS");
}
API
device.width // 设备分辨率
device.height
text([str])
desc([str])
className([str])
id([str])
bounds() // 控件范围(返回Rect)
// 例:Rect.left
// 例:Rect.right
// 例:Rect.top
// 例:Rect.bottom
// 例:Rect.centerX()
// 例:Rect.centerY)
// 例:Rect.width()
// 例:Rect.height()
depth([num]) // 控件的布局深度
// 例:className("ImageView").depth(10).findOne()
size() // find()的数量
get([index]) // find()第i个控件
each([func]) // 遍历find()
findOne() // 直到找到(第一个)
findOnce() // 只找一次
untilFind() // 直到找到第一个(返回所有)
find() // 只找一次(返回所有)(阻塞)
waitFor() // 等待出现(阻塞)
exits() // 判断控件是否存在
filter([func]) // 过滤
children() // 子控件集合
childCount() // 子控件数量
child([index]) // 子控件
parent() // 父控件
empty() // 控件集合是否 为空
nonEmpty() // 控件集合是否 非空
[node].click() // 点击
click([str]) // 点击文本
click([num, num]) // 点击坐标 (安卓7.0+)
clickCenter() // 点击控件的中间
longClick() // 长按
swipe([x1, y1, x2, y2, 时长]) // 滑动 (安卓7.0+)
copy() // 复制 输入框选中内容
cut() // 剪切 输入框选中内容
paste() // 粘贴内容到输入框
select() // 选中控件(选择框)
scrollUp([index]) // 上划或左滑(没有参数滑动最大的)
scrollDown([index]) // 下划或右滑(没有参数滑动最大的)
setText([index, str]) // 设置输入框内容(不加索引,设置全部输入框)(覆盖原内容)
input([index, str]) // 设置输入框内容(不加索引,设置全部输入框)(追加内容)
KeyCode([code]) // 要按下的按键(名称或代码 例:KEYCODE_0)
back() // 返回键
home() // home键
powerDialog() // 弹出电源键菜单
notifications() // 拉出通知栏
quickSettings() // 显示快速设置(下拉通知栏到底)
recents() // 显示最近任务
多线程
threads.start(function() {}); // 启动线程,返回thread实例(用于)
[thread].interrup() // 停止线程
threads.shutDownAll() // 通知所有start开启的线程
UI
autojs
$ui.layout(
<vertical>
<webview id="web" w="*" h="*" />
</vertical>
);
// const webName = $files.join($files.cwd(), 'web')
// $ui.web.loadUrl( `file://${webName}/web/index.html` );
$ui.web.loadUrl(`http://192.168.91.38:5500/web/index.html`);
// 接收
const jsBridge = $ui.web.jsBridge;
jsBridge.handle('open', () => {
toastLog('打开...')
});
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="autojs://sdk/v1.js"></script>
</head>
<body>
<button id="btn">open</button>
<script>
document.getElementById('btn').onclick = function() {
// 发送事件请求
$autojs.invoke('open')
}
</script>
</body>
</html>