-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy path广播发送接收(示例).js
80 lines (73 loc) · 2.28 KB
/
广播发送接收(示例).js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"ui";
ui.layout(
<vertical gravity="center">
<text textSize="24sp" gravity="top|center" text="广播接收发射器"/>
<text marginTop="12sp" textSize="20sp" text="编辑发送内容"/>
<input id="extra" hint="请输入发送的广播内容"/>
<button id="registered" text="注册广播"/>
<button id="send" text="发送广播"/>
<button id="receive" text="接收广播"/>
<button id="destroy" text="销毁广播"/>
</vertical>
);
importClass(android.content.ContextWrapper);
importClass(android.content.BroadcastReceiver);
importClass(android.content.IntentFilter);
//-<>无名小姐:1352183717<---<<>>//
/**
*这是一个简单的广播动态注册发送的例子
*其实在广播注册那一步已经做好了接收准备了
*(相当于一个监听器吧)
*我为了更直观一些把它分解开来了
*后面才打印出来方便观察
*可能有bug,毕竟健壮性不是很强
*----页面将就吧-----
*/
var test, clear;
var is = true;
ui.send.click(function() {
var get = ui.extra.text();
if (!is) {
if (get != "") {
var intent = new Intent();
intent.setAction("BroadcastReceiver")
intent.putExtra("information", get + "");
context.sendBroadcast(intent);
toastLog("发送成功");
is = false;
} else {
toastLog("请输入要发送的信息");
}
} else {
toastLog("请先注册广播")
}
});
ui.registered.click(function() {
var filter = new IntentFilter();
filter.addAction("BroadcastReceiver");
new ContextWrapper(context).registerReceiver(clear = new BroadcastReceiver({
onReceive: function(context, intent) {
test = intent.getStringExtra("information");
}
}), filter);
toastLog("注册广播成功")
is = false;
});
ui.receive.click(function() {
toastLog("广播接收到内容:" + test);
});
ui.destroy.click(function() {
if (clear != null) {
new ContextWrapper(context).unregisterReceiver(clear);
}
clear = null;
test = undefined;
toastLog("已注销广播")
});
events.on("exit", function() {
if (clear != null) {
new ContextWrapper(context).unregisterReceiver(clear);
}
clear = null;
log("结束运行");
});