当前位置:网站首页>BroadCast Receiver(广播)详解
BroadCast Receiver(广播)详解
2022-08-05 05:15:00 【suiyue010211】
目录
2.在AndroidManifest.xml中写入静态接收名
BroadCast Receiver是四大组件之一,是一个全局的监听器
BroadCast Receiver有两大功能,广播发送者,广播接收者
BroadCast Receiver分为静态广播和动态动态广播
静态广播
1.首先在这边创建广播接收器
2.在AndroidManifest.xml中写入静态接收名
<receiver
android:name=".MyStaiticReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="wzk" />
</intent-filter>
</receiver>
3.在主方法把广播发送给静态广播接收者
btn_sendstatic=findViewById(R.id.btn_sendstatic);
btn_sendstatic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//发送广播给静态广播接收者
Intent intent=new Intent();
//在Android8.0以上就不支持静态广播了,需要加一个这个
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) {
//发送广播给静态广播接收者
Intent intent=new Intent();
intent.setPackage(getPackageName());
intent.setAction("wzk");
intent.putExtra("info","你好");
sendBroadcast(intent);
}
});
4.在广播接收器里面接收广播
Log.i("MyStaiticReceiver","接收到的值是"+intent.getStringExtra("info"));
动态广播:
1.创建广播继承BroadcastReceiver
class MyDymnicReveiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Log.i("动态广播接收者",intent.getStringExtra("info"));
}
}
2.在主方法注册动态广播
//注册动态广播
myDymnicReveiver=new MyDymnicReveiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("666");
registerReceiver(myDymnicReveiver,intentFilter);
3.发送给动态广播
//发送给动态的广播
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","动态广播你好");
sendBroadcast(intent);
}
});
4.反注册
@Override
protected void onDestroy() {
super.onDestroy();
if (myDymnicReveiver!=null){
unregisterReceiver(myDymnicReveiver);
}
}
边栏推荐
- el-table,el-table-column,selection,获取多选选中的数据
- 周末作业-循环练习题(2)
- 大型Web网站高并发架构方案
- Day1:用原生JS把你的设备变成一台架子鼓!
- Map、WeakMap
- 【MySQL】数据库多表链接的查询方式
- The role of the range function
- [Remember 1] June 29, 2022 Brother and brother double pain
- In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
- 如何编写一个优雅的Shell脚本(三)
猜你喜欢
Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution
[Go through 10] sklearn usage record
[Go through 7] Notes from the first section of the fully connected neural network video
flink中文文档-目录v1.4
Matplotlib(三)—— 实践
【过一下14】自习室的一天
Matplotlib(二)—— 子图
pycharm中调用Matlab配置:No module named ‘matlab.engine‘; ‘matlab‘ is not a package
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
【过一下3】卷积&图像噪音&边缘&纹理
随机推荐
flink项目开发-配置jar依赖,连接器,类库
转正菜鸟前进中的经验(废话)之谈 持续更新中... ...
第二讲 Linear Model 线性模型
BFC(Block Formatting Context)
如何停止flink job
关于基于若依框架的路由跳转
软件设计 实验四 桥接模式实验
[Over 17] Pytorch rewrites keras
怎么更改el-table-column的边框线
Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
BFC详解(Block Formmating Context)
学习总结week2_1
Machine Learning (2) - Machine Learning Fundamentals
flink项目开发-flink的scala shell命令行交互模式开发
[Redis] Resid的删除策略
Day1:用原生JS把你的设备变成一台架子鼓!
The role of the range function
CAP+BASE
Flink HA安装配置实战
物理层的接口有哪几个方面的特性?各包含些什么内容?