ADB Shell 与 Kotlin 调用示例
ADB Shell 调用
电脑通过 USB 连接设备,用 adb shell 的 am broadcast 命令发送 Intent 广播:
# 隐藏指定包名的应用
am broadcast -a com.bintianqi.owndroid.action.HIDE ^
-n com.bintianqi.owndroid/.ApiReceiver ^
--es key YOUR_API_KEY ^
--es package com.example.app
# 挂起应用(Android 7+)
am broadcast -a com.bintianqi.owndroid.action.SUSPEND ^
-n com.bintianqi.owndroid/.ApiReceiver ^
--es key YOUR_API_KEY ^
--es package com.game.app
# 禁用摄像头
am broadcast -a com.bintianqi.owndroid.action.SET_CAMERA_DISABLED ^
-n com.bintianqi.owndroid/.ApiReceiver ^
--es key YOUR_API_KEY
# 禁止截屏录屏
am broadcast -a com.bintianqi.owndroid.action.SET_SCREEN_CAPTURE_DISABLED ^
-n com.bintianqi.owndroid/.ApiReceiver ^
--es key YOUR_API_KEY
# 清除应用存储(Android 9+)
am broadcast -a com.bintianqi.owndroid.action.CLEAR_APP_STORAGE ^
-n com.bintianqi.owndroid/.ApiReceiver ^
--es key YOUR_API_KEY ^
--es package com.example.app
# 批量隐藏多个应用(多个 --es package 参数)
am broadcast -a com.bintianqi.owndroid.action.HIDE ^
-n com.bintianqi.owndroid/.ApiReceiver ^
--es key YOUR_API_KEY ^
--es package com.game.app ^
--es package com.social.app ^
--es package com.shopping.app
# 开启禁止安装任何应用用户限制
am broadcast -a com.bintianqi.owndroid.action.ADD_USER_RESTRICTION ^
-n com.bintianqi.owndroid/.ApiReceiver ^
--es key YOUR_API_KEY ^
--es restriction no_install_apps
# 设备锁屏
am broadcast -a com.bintianqi.owndroid.action.LOCK ^
-n com.bintianqi.owndroid/.ApiReceiver ^
--es key YOUR_API_KEY
# 设备重启(Android 7+)
am broadcast -a com.bintianqi.owndroid.action.REBOOT ^
-n com.bintianqi.owndroid/.ApiReceiver ^
--es key YOUR_API_KEY
Windows PowerShell 里可以用 ^ 换行,或者写成单行。
Kotlin 调用
从任何 Android App 内部发送广播:
// 挂起 com.game.app
val intent = Intent("com.bintianqi.owndroid.action.SUSPEND")
.setComponent(ComponentName(
"com.bintianqi.owndroid",
"com.bintianqi.owndroid.ApiReceiver"
))
.putExtra("key", "YOUR_API_KEY")
.putExtra("package", "com.game.app")
context.sendBroadcast(intent)
// 授予权限
val grantIntent = Intent("com.bintianqi.owndroid.action.SET_PERMISSION_GRANTED")
.setComponent(ComponentName("com.bintianqi.owndroid", "com.bintianqi.owndroid.ApiReceiver"))
.putExtra("key", "YOUR_API_KEY")
.putExtra("package", "com.camera.app")
.putExtra("permission", "android.permission.CAMERA")
context.sendBroadcast(grantIntent)
// 禁用摄像头
val cameraIntent = Intent("com.bintianqi.owndroid.action.SET_CAMERA_DISABLED")
.setComponent(ComponentName("com.bintianqi.owndroid", "com.bintianqi.owndroid.ApiReceiver"))
.putExtra("key", "YOUR_API_KEY")
context.sendBroadcast(cameraIntent)
Tasker / Automate 集成
Tasker 和 Automate 这类自动化工具可以发送自定义 Intent 广播——填入 OwnDroid 的 Action、Component、Extras 即可实现自动化触发。比如 Tasker 里创建一个 Profile:
- 触发条件:每天晚上 22:00
- 执行动作:发送 Intent broadcast(Action=
com.bintianqi.owndroid.action.SUSPEND,Extras: key=你的密钥, package=游戏包名)
效果:每天晚上 10 点自动挂起游戏,防止睡前熬夜打游戏。第二天早上 7 点自动 UNSUSPEND。
常见问题
- am broadcast 返回 0 但功能没生效:OwnDroid 的 ApiReceiver 是异步处理,可能需要 1-2 秒。也可能是 Device Owner 权限不够(Dhizuku 模式下某些 API 不可用)
- 多个 package 参数:ADB 的
--es可以多次使用同一个 key,最后一个会覆盖前面的。如果需要批量隐藏多个应用,需要多次调用 am broadcast - API 密钥为空:OwnDroid 收到没有 key extra 的广播会直接忽略,不报错也不执行