Skip to content

Conversation

@whjstc
Copy link

@whjstc whjstc commented Dec 11, 2025

🎯 Problem

ExternalControlActivity causes screen flash/popup on some ROMs (especially Flyme, MIUI, ColorOS) when triggered by automation tools like Tasker, breaking the "background automation" experience.

✅ Solution

This PR adds two components for reliable background automation:

1. ExternalControlReceiver (BroadcastReceiver)

  • Receives broadcasts from Tasker/automation tools
  • Completely background operation - no UI triggered
  • Supports START/STOP/TOGGLE actions

2. KeepAliveService (Foreground Service)

Why this is needed: During testing, I discovered that BroadcastReceiver fails when the app enters deep sleep (CACHED_EMPTY state, procState=19). The system freezes the app and blocks broadcasts from third-party apps.

Solution: A lightweight foreground service that:

  • Keeps the app alive to reliably receive broadcasts
  • Uses IMPORTANCE_MIN notification channel (minimal user disturbance)
  • Idle service with near-zero battery impact
  • Automatically starts with the app

🧪 Testing

Tested extensively on Flyme with Tasker:

  • ✅ 100% reliable after hours in background
  • ✅ No screen flash or popup
  • ✅ App remains responsive to broadcasts even in deep background
  • ✅ Process not frozen (procState improved from 19→4, oom_adj from 955→50)
  • ✅ Minimal battery impact (idle foreground service)

📱 Usage

Tasker Configuration

Action: Send Intent

  • Action: com.github.metacubex.clash.meta.action.START_CLASH (or STOP/TOGGLE)
  • Target: Broadcast Receiver
  • Package: com.github.metacubex.clash.meta
  • Class: (leave empty)

Example Automation Scenarios

  1. Auto-stop on home WiFi: Disable proxy when connected to trusted network
  2. Auto-start on mobile data: Enable proxy when leaving home WiFi
  3. Time-based control: Disable proxy during specific hours
  4. Charging state: Enable proxy only when charging

📄 Files Changed

  • app/src/main/java/.../ExternalControlReceiver.kt - BroadcastReceiver implementation
  • app/src/main/java/.../KeepAliveService.kt - Foreground service for reliability
  • app/src/main/java/.../MainApplication.kt - Auto-start KeepAliveService
  • app/src/main/AndroidManifest.xml - Register receiver and service
  • TASKER_GUIDE.md - Complete user guide with examples
  • app/src/main/res/values/strings.xml - Notification strings

🔧 Technical Details

Why Foreground Service instead of battery optimization whitelist?

  • Battery optimization is unreliable and ROM-dependent
  • Foreground Service is the Android-official way to keep apps alive
  • Used by all apps needing background reliability (music players, navigation, etc.)

Resource usage:

  • CPU: 0% (idle service)
  • Network: 0
  • Battery: Negligible (no active operations)
  • Memory: ~10-20MB (process kept in memory)

📚 Documentation

Complete Tasker setup guide included in TASKER_GUIDE.md with:

  • Step-by-step configuration
  • Common automation scenarios
  • Troubleshooting tips
  • FAQ

Add ExternalControlReceiver for Tasker/automation tools to control
Clash service completely in background without triggering any UI.

This solves the issue where ExternalControlActivity causes screen
flash/popup on some ROMs (Flyme, MIUI, ColorOS, etc.) when triggered
by automation tools.

Features:
- Works on all ROMs without triggering UI
- No screen flash or popup
- Same actions as ExternalControlActivity (START/STOP/TOGGLE)
- Complete Tasker configuration guide included

Usage in Tasker:
- Action: com.github.metacubex.clash.meta.action.START_CLASH
- Target: Broadcast Receiver
- Package: com.github.metacubex.clash.meta

Files changed:
- app/src/main/java/.../ExternalControlReceiver.kt (new)
- app/src/main/AndroidManifest.xml (register receiver)
- TASKER_GUIDE.md (user documentation)
修复两处乱码:
- '保存' 字符显示异常
- '弹出' 字符显示异常
问题:
- ExternalControlReceiver 在应用后台休眠时无法接收 Tasker 广播
- 应用进入 CACHED_EMPTY 状态(procState=19)时会被系统冻结
- 第三方应用发送的广播无法唤醒深度休眠的应用

解决方案:
- 添加轻量级 Foreground Service (KeepAliveService)
- 提升进程优先级(从 955 降至 50),防止进入深度休眠
- 使用 IMPORTANCE_MIN 通知渠道,最小化对用户的干扰
- 应用启动时自动启动服务

技术细节:
- KeepAliveService: 空服务,仅用于保持进程活跃,几乎不耗电
- 通知文案:标题"Clash 服务",内容"运行中"
- 在 MainApplication.onCreate 中自动启动

测试结果:
- Tasker 广播接收 100% 可靠
- 应用在后台长时间待机后仍能正常响应
- 进程不会被冻结(isFrozen=0)

相关文件:
- KeepAliveService.kt: 核心服务实现
- MainApplication.kt: 自动启动服务
- AndroidManifest.xml: 注册服务和更新注释
- TASKER_GUIDE.md: 更新文档说明
@whjstc whjstc changed the title feat: Add BroadcastReceiver for background automation control feat: Add BroadcastReceiver with KeepAliveService for reliable automation Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant