当前位置:网站首页>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);
}
}边栏推荐
- 怎样在Disciples门徒获得收益?
- CVPR best paper winner Huang Gao's team from Tsinghua University presented the first dynamic network review
- day9-字符串作业
- BFC(Block Formatting Context)
- Do you use tomatoes to supervise your peers?Add my study room, come on together
- 基于Flink CDC实现实时数据采集(二)-Source接口实现
- 对数据排序
- [Skill] Long-term update
- Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent
- Oracle压缩表修改字段的处理方法
猜你喜欢

BroadCast Receiver(广播)详解

【论文精读】R-CNN 之预测框回归(Bounding box regression)问题详述

Tensorflow踩坑笔记,记录各种报错和解决方法

发顶会顶刊论文,你应该这样写作

华科提出首个用于伪装实例分割的一阶段框架OSFormer

【零基础开发NFT智能合约】如何使用工具自动生成NFT智能合约带白名单可Mint无需写代码

【Kaggle项目实战记录】一个图片分类项目的步骤和思路分享——以树叶分类为例(用Pytorch)

【Pytorch学习笔记】9.分类器的分类结果如何评估——使用混淆矩阵、F1-score、ROC曲线、PR曲线等(以Softmax二分类为例)

【Pytorch学习笔记】8.训练类别不均衡数据时,如何使用WeightedRandomSampler(权重采样器)

解决:Unknown column ‘id‘ in ‘where clause‘ 问题
随机推荐
flink部署操作-flink standalone集群安装部署
[Go through 4] 09-10_Classic network analysis
el-pagination左右箭头替换成文字上一页和下一页
【数据库和SQL学习笔记】7.SQL中的插入(INSERT)、删除(DELETE)、更新(UPDATE)
Flink 状态与容错 ( state 和 Fault Tolerance)
el-table鼠标移入表格改变显示背景色
学习总结week2_4
实现跨域的几种方式
Flink和Spark中文乱码问题
SparkML-初探-文本分类
ES6基础语法
【Pytorch学习笔记】11.取Dataset的子集、给Dataset打乱顺序的方法(使用Subset、random_split)
华科提出首个用于伪装实例分割的一阶段框架OSFormer
ES6 Set、WeakSet
[Go through 10] sklearn usage record
全尺度表示的上下文非局部对齐
[Go through 8] Fully Connected Neural Network Video Notes
[After a 12] No record for a whole week
怎样在Disciples门徒获得收益?
BFC详解(Block Formmating Context)