当前位置:网站首页>Detailed explanation of BroadCast Receiver (broadcast)
Detailed explanation of BroadCast Receiver (broadcast)
2022-08-05 05:38:00 【suiyue010211】
目录
1.First create the broadcast receiver here
2.在AndroidManifest.xmlWrite the static receive name in
3.Send the broadcast to the static broadcast receiver in the main method
4.Receive broadcasts in a broadcast receiver
1.Create broadcast inheritanceBroadcastReceiver
2.Register the dynamic broadcast in the main method

BroadCast Receiver是四大组件之一,是一个全局的监听器
BroadCast Receiver有两大功能,广播发送者,广播接收者
BroadCast ReceiverDivided into static broadcasting and dynamic dynamic broadcasting
静态广播
1.First create the broadcast receiver here

2.在AndroidManifest.xmlWrite the static receive name in
<receiver
android:name=".MyStaiticReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="wzk" />
</intent-filter>
</receiver>3.Send the broadcast to the static broadcast receiver in the main method
btn_sendstatic=findViewById(R.id.btn_sendstatic);
btn_sendstatic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Send a broadcast to static broadcast receivers
Intent intent=new Intent();
//在Android8.0The above does not support static broadcasting,Need to add this one
intent.setPackage(getPackageName());
intent.setAction("wzk");
intent.putExtra("info","你好");
sendBroadcast(intent);
}
}); btn_sendstatic=findViewById(R.id.btn_sendstatic);
btn_sendstatic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Send a broadcast to static broadcast receivers
Intent intent=new Intent();
intent.setPackage(getPackageName());
intent.setAction("wzk");
intent.putExtra("info","你好");
sendBroadcast(intent);
}
});4.Receive broadcasts in a broadcast receiver
Log.i("MyStaiticReceiver","接收到的值是"+intent.getStringExtra("info"));动态广播:
1.Create broadcast inheritanceBroadcastReceiver
class MyDymnicReveiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("动态广播接收者",intent.getStringExtra("info"));
}
}2.Register the dynamic broadcast in the main method
//注册动态广播
myDymnicReveiver=new MyDymnicReveiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("666");
registerReceiver(myDymnicReveiver,intentFilter);3.Sent to dynamic broadcast
//Send to dynamic broadcast
btn_send = findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setAction("666");
intent.putExtra("info","dynamic broadcast hello");
sendBroadcast(intent);
}
});4.反注册
@Override
protected void onDestroy() {
super.onDestroy();
if (myDymnicReveiver!=null){
unregisterReceiver(myDymnicReveiver);
}
}边栏推荐
- 转正菜鸟前进中的经验(废话)之谈 持续更新中... ...
- SQL (2) - join window function view
- Lecture 5 Using pytorch to implement linear regression
- 轻松接入Azure AD+Oauth2 实现 SSO
- flink on yarn 集群模式启动报错及解决方案汇总
- DOM and its applications
- 如何编写一个优雅的Shell脚本(二)
- 【Over 16】Looking back at July
- 用GAN的方法来进行图片匹配!休斯顿大学提出用于文本图像匹配的对抗表示学习,消除模态差异!
- Do you use tomatoes to supervise your peers?Add my study room, come on together
猜你喜欢
随机推荐
It turns out that the MAE proposed by He Yuming is still a kind of data enhancement
MySQL
基于Flink CDC实现实时数据采集(三)-Function接口实现
ECCV2022 | RU&谷歌提出用CLIP进行zero-shot目标检测!
通过Flink-Sql将Kafka数据写入HDFS
【数据库和SQL学习笔记】3.数据操纵语言(DML)、SELECT查询初阶用法
【Over 15】A week of learning lstm
SSL 证书签发详细攻略
如何编写一个优雅的Shell脚本(一)
es6迭代协议
[Go through 7] Notes from the first section of the fully connected neural network video
学习总结week3_3迭代器_模块
ES6 新特性:Class 的继承
npm搭建本地服务器,直接运行build后的目录
神经网络也能像人类利用外围视觉一样观察图像
[Pytorch study notes] 9. How to evaluate the classification results of the classifier - using confusion matrix, F1-score, ROC curve, PR curve, etc. (taking Softmax binary classification as an example)
服务网格istio 1.12.x安装
【零基础开发NFT智能合约】如何使用工具自动生成NFT智能合约带白名单可Mint无需写代码
MaskDistill-不需要标注数据的语义分割
vscode要安装的插件






![[Go through 10] sklearn usage record](/img/70/60783c7d16000c6e9d753d8db9a330.png)

