一、功能说明
本代码用于自动点击 Android 系统截图权限确认弹窗中的「允许」按钮,适用于 MIUI 等需要二次确认的系统环境。通过后台线程实时监控弹窗,确保 Hamibot 顺利获取屏幕截图权限。
二、使用步骤
基础使用
// 将以下代码粘贴到脚本开头
threads.start(function () {
const check = () => {
let activity = currentActivity().toLowerCase();
if (activity.indexOf('permission') !== -1) {
if (
id('com.android.systemui:id/dialog_title')
.textContains('Hamibot')
.exists()
) {
id('android:id/button1').findOnce().click(); // 点击确认按钮
return;
}
}
setTimeout(check, 500); // 每 500ms 检查一次
};
check();
});
if (!requestScreenCapture()) {
hamibot.exit(); // 权限获取失败时退出
}
threads.shutDownAll(); // 关闭监控线程
sleep(1000); // 等待弹窗消失
添加你的业务代码
// 在下方添加你的主要脚本逻辑
let img = captureScreen();
// 这里继续编写图像处理/自动化操作代码...
三、代码解析
线程监控模块
threads.start()
:创建后台线程避免阻塞主程序
currentActivity()
:获取当前界面 Activity 信息
textContains('Hamibot')
:识别包含指定文字的弹窗标题
点击逻辑
id('android:id/button1')
:通过控件 ID 定位确认按钮
findOnce().click()
:执行点击操作后终止检查
权限验证
requestScreenCapture()
:返回值判断权限状态
hamibot.exit()
:权限获取失败时安全退出
四、适配建议
多系统适配(示例代码)
// 华为 EMUI 系统适配
if (id('android:id/alertTitle').textContains('屏幕截图').exists()) {
click('允许');
}
// OPPO ColorOS 适配
if (textMatches(/屏幕录制/).exists()) {
click('立即开始');
}
增强稳定性
// 增加点击成功率检测
let btn = id('android:id/button1').findOnce();
if (btn && btn.click()) {
log('点击成功');
return;
}
五、注意事项
- 不同机型需要调整控件定位参数,建议使用「布局分析」或「设备调试」功能获取准确控件信息
- 保持
threads.shutDownAll()
调用,避免残留线程影响脚本执行 - 若遇到无法识别的弹窗,可通过
toastLog(activity)
输出当前界面信息辅助调试
完整代码
// 启用线程,点击截图确认按钮
threads.start(function () {
const check = () => {
// MIUI 系统
let activity = currentActivity().toLowerCase();
if (activity.indexOf('permission') !== -1) {
if (
id('com.android.systemui:id/dialog_title')
.textContains('Hamibot')
.exists()
) {
// TODO 点击确认
if (id('android:id/button1').findOnce().click()) {
return; // 点击成功,不再检查
}
}
}
// TODO 检查其他系统
// 未确认,继续检查
setTimeout(function () {
check();
}, 500);
};
check();
});
if (!requestScreenCapture()) {
hamibot.exit(); // 没获得授权,退出
}
threads.shutDownAll(); // 结束线程
sleep(1000); // 可选,短暂延迟,避免截图包含确认框
// TODO 你的其他代码
let img = captureScreen();