意图 Intent
Intent 用来构造并启动 Android Intent。日常按包名打开应用、按 URI 跳转,可优先用 App 的 launch / gotoIntent / openUrl;需要 extras、flags、组件名时用本模块。
Intent.startActivity({
action: 'android.intent.action.VIEW',
data: 'https://doc.deeke.cn'
});| 方法 | 作用 |
|---|---|
create(options) | 构造 android.content.Intent,不启动 |
startActivity(options|Intent) | 启动 Activity |
startService(options) | 启动 Service |
sendBroadcast(options) | 发送广播 |
registerReceiver(action, callback) | 注册接收广播 |
unregisterReceiver(action) | 取消指定 action 接收 |
unregisterAllReceivers() | 取消全部接收 |
open() | 打开本应用详情设置页 |
open(uri) | 用 VIEW 打开 URI |
options 字段
| 字段 | 说明 |
|---|---|
action | Intent action,如 android.intent.action.VIEW |
data | URI |
type | MIME;可与 data 同时设置 |
packageName / package | 目标包名 |
className | 与包名一起指定组件 |
categories | 字符串或字符串数组 |
flags | 数字、字符串名(如 ACTIVITY_NEW_TASK)或数组 |
extras | 键值对象(布尔 / 数字 / 字符串 / 字符串数组) |
startActivity 未带 ACTIVITY_NEW_TASK 时会自动补上。也可直接传入已有 Intent。
create(options)
参数: options {object\|Intent}
返回: {android.content.Intent}
只构造,不启动。可交给 Intent.startActivity 或 App.startActivity。
let i = Intent.create({
action: 'android.intent.action.SEND',
type: 'text/plain',
extras: { 'android.intent.extra.TEXT': 'hello' },
flags: ['ACTIVITY_NEW_TASK']
});startActivity(options|Intent)
参数: options 对象或已有 Intent
返回: {void}
Intent.startActivity({
action: 'android.intent.action.VIEW',
data: 'https://example.com',
flags: 'ACTIVITY_NEW_TASK'
});startService(options)
参数: options {object}
返回: {void}
启动 Android Service(后台组件),不是打开界面。
常见场景:
- 按包名 + 类名启动已知的目标 App Service(对方文档/反编译里能拿到组件名)
- 给 Service 传
action/extras触发对方约定好的任务 - 本工程里若要做「常驻任务」,优先用 前台服务 ForegroundServiceBridge,不要依赖本方法在后台保活
注意: Android 8+ 对后台 startService 限制很严,从后台脚本调用可能抛 IllegalStateException;显式组件(packageName + className)比纯 action 更稳。
// 显式启动某应用的 Service,并带参数
Intent.startService({
packageName: 'com.example.target',
className: 'com.example.target.DemoService',
action: 'com.example.action.DO_WORK',
extras: {
taskId: '123',
silent: true
}
});// 仅指定包内 action(目标 App 需有对应 intent-filter)
Intent.startService({
packageName: 'com.example.target',
action: 'com.example.action.SYNC'
});sendBroadcast(options)
参数: options {object}
返回: {void}
发送 Broadcast(广播),通知已注册的 BroadcastReceiver,不打开界面、也不启动 Service。
常见场景:
- 向指定包发自定义 action,触发对方 Receiver(协作、插件、自家多进程)
- 用本模块 registerReceiver 先注册,再
sendBroadcast自测收发 - 带
extras传简短指令或状态(字符串 / 数字 / 布尔) - 系统级隐式广播在新系统上大多受限,不要指望随便发
android.intent.action.*还能被其它 App 收到
注意: 尽量带 packageName(或再加 className)做成定向广播;隐式广播在 Android 8+ 经常发了也没人收到。
// 定向广播:只发给目标包里匹配该 action 的 Receiver
Intent.sendBroadcast({
packageName: 'com.example.target',
action: 'com.example.action.REFRESH',
extras: {
reason: 'manual',
count: 1
}
});// 显式指定 Receiver 组件(最稳)
Intent.sendBroadcast({
packageName: 'com.example.target',
className: 'com.example.target.CmdReceiver',
action: 'com.example.action.CMD',
extras: { cmd: 'ping' }
});registerReceiver(action, callback)
参数:
action {string\|string[]}要监听的 action(可多个)callback {function}收到广播时回调
返回: {void}
动态注册广播接收。同一 action 再次注册会先卸掉旧监听。脚本结束会自动 unregisterAllReceivers。
回调参数 intent 字段:action、data、type、extras、packageName。
Intent.registerReceiver('com.example.action.REFRESH', function (intent) {
console.log(intent.action, intent.extras);
});
// 自测收发(同应用可省略 packageName;发给其它 App 时请指定其包名)
Intent.sendBroadcast({
action: 'com.example.action.REFRESH',
extras: { reason: 'manual' }
});
setInterval(function () {
console.log('等待广播中');
}, 10000);// 一次监听多个 action
Intent.registerReceiver(['com.example.A', 'com.example.B'], function (intent) {
console.log(intent.action);
});unregisterReceiver(action)
参数: action {string}
返回: {void}
取消该 action 的接收;若当初一次注册了多个 action,整组一起取消。
unregisterAllReceivers()
返回: {void}
取消本脚本全部动态广播接收。
open() / open(uri)
参数:
- 无参:打开本应用详情
uri {string}:VIEW 打开该 URI
返回: {void}
Intent.open();
Intent.open('https://example.com');