系统函数 System
System 是任务脚本里最常用的一组能力:等一会儿、看现在在哪个 Activity、写剪贴板、弹 Toast、切无障碍快慢模式。找节点用 UiSelector,打开别的 App 用 App。
currentActivity / currentPackage 依赖无障碍,没开服务会抛错并提示用户去开。后台弹 Toast 请用 FloatDialogs,见 弹窗。
console.log('立即输出');
System.sleep(1000);
console.log(System.currentPackage());| 方法 | 作用 |
|---|---|
sleep / preciseSleep | 休眠 |
time() | 当前时间字符串 |
currentActivity() / currentPackage() | 当前界面 |
waitForActivity / waitForPackage | 等到指定界面出现 |
setClip / getClip | 剪贴板 |
toast / toastLong | 气泡提示 |
exit() | 关掉脚本引擎 |
setAccessibilityMode 等 | 快 / 慢扫节点 |
setTimeWindowShow / setKeepScreenOn | 运行时间窗、常亮 |
getLocaleInfo | 语言和地区 |
vibrate / cancelVibrate | 振动 |
getVolume / setVolume / getMaxVolume | 音量(music/ring/…) |
getRingerMode / setRingerMode | 0 静音 / 1 振动 / 2 正常 |
shell | 执行 shell,返回 {code,result,error} |
sleep(millisecond)
参数:
millisecond {number}毫秒
返回: {void}
阻塞当前脚本。一般等待用这个即可。
console.log('立即输出');
System.sleep(1000);
console.log('1秒钟后输出');preciseSleep(millisecond)
参数:
millisecond {number}毫秒
返回: {void}
更准的休眠:会用 WakeLock 保持 CPU 醒着,并循环核对时间。更耗电。不要求那么准时时用 sleep。
System.preciseSleep(2000);time()
返回: {string} 当前系统时间,如 2024-03-07 12:12:12
console.log(System.time());currentActivity()
返回: {string} 最近一次监测到的 Activity 名
一般就是当前界面。依赖无障碍。
console.log(System.currentActivity());currentPackage()
返回: {string} 最近一次监测到的包名
依赖无障碍。当前脚本自己的包名见 App.currentPackageName()。
console.log(System.currentPackage());setClip(content)
参数:
content {string}要写入剪贴板的文本
返回: {void}
写入后再对输入框 paste()。
getClip()
返回: {string|null} 剪贴板文本;没有则为 null
toast(message)
参数:
message {string}要显示的文案
返回: {void}
弹出短提示,大约 2 秒,具体看系统。异步执行,不会等它消失。脚本在后台时用 FloatDialogs.toast()。
toastLong(message)
参数:
message {string}要显示的文案
返回: {void}
比 toast 更久。同样异步。后台用 FloatDialogs.toastLong()。
waitForActivity(activity, period, timeout)
参数:
activity {string}要等的 Activity 名period {number}检查间隔(毫秒)timeout {number}最长等待(毫秒)
返回: {boolean} 出现为 true,超时为 false
let found = System.waitForActivity('com.example.MainActivity', 100, 5000);
if (found) {
console.log('Activity已出现');
}waitForPackage(package, period, timeout)
参数:
package {string}要等的包名period {number}检查间隔(毫秒)timeout {number}最长等待(毫秒)
返回: {boolean} 出现为 true,超时为 false
let found = System.waitForPackage('com.example.app', 100, 5000);
if (found) {
console.log('Package已出现');
}exit()
返回: {void}
关掉当前脚本引擎,任务到此结束。
setTimeWindowShow(show)
参数:
show {boolean}是否显示运行时间悬浮窗
返回: {void}
截图被时间窗挡住时,先关掉再截。
System.setTimeWindowShow(false);setAccessibilityMode(mode)
参数:
mode {string}'fast'为快速模式;其它值切回正常模式
返回: {void}
立即生效,不用重启无障碍。快速模式会丢掉一批不重要的节点,查找更快。按 id 或 text 找节点不受影响。大多数时候用快速模式;某个节点拿不到时切回正常,处理完再切回去。
System.setAccessibilityMode('fast');
System.setAccessibilityMode('!fast');getAccessibilityMode()
返回: {string} "fast" 或 "normal"
let mode = System.getAccessibilityMode();
console.log('当前模式:' + mode);isAccessibilityQuick()
返回: {boolean} 当前是否为快速扫描
if (System.isAccessibilityQuick()) {
console.log('当前为快速扫描模式');
}setKeepScreenOn(keepOn)
参数:
keepOn {boolean}是否保持常亮
返回: {void}
靠右侧悬浮窗实现。没有悬浮窗权限、或把右侧窗藏掉时,这个方法不生效。
System.setKeepScreenOn(true);
System.setKeepScreenOn(false);getLocaleInfo()
返回: {object} 当前语言和地区(配置里的第一个 Locale)
需要 Android 7.0(API 24)及以上。字段:
language {string}ISO 639-1,如"zh"country {string}ISO 3166-1,如"CN",有的环境是空串tag {string}BCP 47,如"zh-CN"
let locale = System.getLocaleInfo();
console.log(locale.language);
console.log(locale.country);
console.log(locale.tag);vibrate / 音量 / shell
System.vibrate(200);
System.setVolume('music', 5);
console.log(System.getVolume(), System.getMaxVolume());
System.setRingerMode(1); // 振动
let r = System.shell('getprop ro.build.version.release');
console.log(r.code, r.result, r.error);
// root 设备:System.shell('pm list packages', true);