|
马上注册,解锁更多高级玩法
您需要 登录 才可以下载或查看,没有账号?立即注册
×
1.注册广播
[JavaScript] 纯文本查看 复制代码 importClass(android.content.IntentFilter)
importClass(android.content.Intent)
let filter = new IntentFilter()
filter.addAction(Intent.ACTION_BATTERY_CHANGED)
filter.addAction("android.intent.action.Mr_老冷")
context.registerReceiver(receiver, filter)
2.监听广播
[JavaScript] 纯文本查看 复制代码 let receiver = new JavaAdapter(android.content.BroadcastReceiver, {
onReceive: function (context, intent) {
logd(intent)
switch (intent.action + "") {
case Intent.ACTION_BATTERY_CHANGED + "":
logd("ACTION_BATTERY_CHANGED")
let level = intent.getIntExtra("level", 0)
logd("当前电量:" + level + "%")
break
case "android.intent.action.Mr_老冷":
logd("===========================")
logd("Mr_老冷自定义广播")
logd("接收到数据 author: " + intent.getStringExtra("author"))
logd("接收到数据 ads: " + intent.getStringExtra("ads"))
break
}
},
})
3.解除监听
[JavaScript] 纯文本查看 复制代码 setStopCallback(function () {
unregisterReceiver()
})
function unregisterReceiver() {
receiver && context.unregisterReceiver(receiver)
receiver = null
logd("关闭广播")
}
4.发送广播
[JavaScript] 纯文本查看 复制代码 importClass(android.content.Intent)
context.sendBroadcast(
new Intent("android.intent.action.Mr_老冷")
.putExtra("author", "Mr_老冷")
.putExtra("ads", "安卓/IOS脚本教学了解一下")
)
完整代码
EC广播示例.js
(1.81 KB, 下载次数: 15, 售价: 5 冰块)
|
|