cron-send-preconditions.md 4.19 KB

Cron Job Preconditions for WeChat Image Sending

Two paths, two failure modes

路径 方法 需屏幕亮? 需微信登录? 典型失败原因
Hook 广播 send_image_via_hook 否 是(必须主动验证) 微信进程不在、登录态过期、hook 未注册
UI 模拟 send_image_via_intent 是 是 屏幕 Dozing、锁屏、联系人搜索匹配错误

日报发送 cron job 应走 hook 路径,不依赖屏幕状态。

Hook 路径(send_image_via_hook)预检 — 三个必须

1. 微信进程运行中(必须)

pidof com.tencent.mm

无输出 → 微信不在。不要继续。

2. 微信已登录(必须)⚠️ 2026-07-25 新增

su -c 'dumpsys activity activities | grep topResumedActivity | grep LauncherUI'

无 LauncherUI → 微信可能卡在 LoginPasswordUI 或 LoginAsExDeviceUI(副设备过期)。 这是最常见的静默失败根因:broadcast 返回 result=0 但微信没登录,图片发不出去。

3. 图片可读

源文件存在且 root 可读。hook 会自动 stage 到 /sdcard/Pictures/ 并触发 media scan。

4. su 可用

广播以 root 身份发送(RECEIVER_NOT_EXPORTED 要求)。

为什么 send_image_via_hook 的自动检测不够

send_image_via_hook 内置的 _wechat_running() 只检查 pidof(进程存在),不检查登录态。 _start_wechat() 只能 am start 启动微信进程,不能保证微信自动登录或 hook 已注册 BroadcastReceiver。

结论:调用方必须在调用 send_image_via_hook 前独立完成 Step 0(进程 + 登录态)。

UI 路径(send_image_via_intent)预检

# Using MCP tools
status = get_lock_status()
# Expected active state: {"screen_on": true, "wakefulness": "Awake"}
# Problem state: {"screen_on": false, "wakefulness": "Dozing"}

Preconditions Checklist

Before starting the send flow, verify:

  1. WeChat 进程运行 + 已登录: pidof + dumpsys LauncherUI 双重验证(hook 路径最关键的步骤)
  2. Screen is on: screen_on: true in lock status(UI 路径)
  3. Device is awake: wakefulness: "Awake"(UI 路径)
  4. Image is in public directory: /sdcard/Pictures/ or /sdcard/Download/
  5. ADBKeyboard is available: For Chinese text input in search(UI 路径)

Cron Job Strategy

def send_digest_if_wechat_online(image_path, contact):
    import subprocess
    # Check 1: WeChat process running
    r = subprocess.run(['pidof', 'com.tencent.mm'], capture_output=True, text=True)
    if not r.stdout.strip():
        print("[SKIP] WeChat process not running")
        return False
    # Check 2: WeChat is on main UI (logged in)
    r = subprocess.run(
        ['su', '-c', 'dumpsys activity activities | grep topResumedActivity | grep LauncherUI'],
        capture_output=True, text=True
    )
    if 'LauncherUI' not in r.stdout:
        print("[SKIP] WeChat not logged in (not on LauncherUI)")
        return False
    # Send
    from wechat_hook_send import send_image_via_hook
    result = send_image_via_hook(contact, image_path)
    return result['status'] == 'sent'

Lessons

2026-07-25: 日报发送静默失败——微信不在线

  • 日报图片 cron job (9e4c5ab0889c) 凌晨 3:00 报 error
  • 根因:微信进程不在,send_image_via_hook 虽然能 am start 启动微信,但微信在 Pixel 上启动后秒崩,hook BroadcastReceiver 未注册
  • broadcast 返回 result=0(系统接受了广播),但图片没发出——静默假成功
  • 修正:所有微信发送操作前必须主动做双重验证(pidof + LauncherUI),不依赖 hook 内置检测

2026-06-26: 屏幕 Dozing 导致 UI 发送失败

  • Generated report and infographic successfully (no screen needed)
  • Failed at send step because device was in Dozing state
  • 20+ tap attempts at center screen did not wake device
  • Screenshots confirmed all-black screen (15KB PNG = black)
  • Conclusion: For cron jobs, separate "generate" from "send" phases. Generate always works. Send only when WeChat is online (hook path) or device is active (UI path).