当前位置:网站首页>BroadCast(广播)
BroadCast(广播)
2022-07-23 21:08:00 【shuo277】
android的四大组件:
Activity、Service、BroadCast(广播)、ContentProvider
BroadCast Receiver
四大组件之一,必须在清单(androidManifest.xml)文件中进行注册
是在组件之间传播数据(Intent)的一种机制
APP哪块用到了广播?
电话
短信
电量
WIFI是否连接
Android中出现的第一个错误?
在组件中进行耗时操作,就会报application no response (ANR)
广播接收者一种有几种?
标准广播
动态注册 使用java代码注册
静态注册 直接在androidManifest.xml注册
有序广播
实现简单的静态注册
1.创建广播接收者

2.在AndroidManifest.xml中对广播接收者进行注册
//注册动态广播接受者
MyDymnicReceiver myDymnicReceiver=new MyDymnicReceiver();
IntentFilter intentFilter=new IntentFilter();
intentFilter.addAction("HOPU2");
registerReceiver(myDymnicReceiver,intentFilter);
3.发送广播
//发送广播给静态接受者
Intent intent=new Intent();
intent.setPackage(getPackageName());
intent.setAction("HOPU");
intent.putExtra("info","你好你好");
sendBroadcast(intent);实现
简单的动态注册
//声明容器 private Button button,button2; //创建内部内 class MyDymnicReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.i("动态广播接受者",intent.getStringExtra("info")); } } //注册动态广播接受者 MyDymnicReceiver myDymnicReceiver=new MyDymnicReceiver(); IntentFilter intentFilter=new IntentFilter(); intentFilter.addAction("HOPU2"); registerReceiver(myDymnicReceiver,intentFilter);//发送动态广播接受者 button2=findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(); intent.setAction("HOPU2"); intent.putExtra("info","hello hello"); sendBroadcast(intent); }
边栏推荐
- TROPOMI(哨兵5P)数据介绍及下载方法
- 【创建 Birthday Card 应用】
- [attack and defense world web] difficulty four-star 12 point advanced question: flatscience
- MySQL(3)
- Quick connect selection recommendation: what are the potential opportunities in the Korean market?
- Be a professional software craftsman
- Green Tao theorem (3): anti uniform functions and their generated sigma Algebras
- 初识js(适合新手的编程)
- OpenCV图像处理——拉普拉斯金字塔
- 1062 Talent and Virtue
猜你喜欢

【Scratch画图100例】图46-scratch绘制花朵 少儿编程 scratch编程画图案例教程 考级比赛画图集训案例

MySql的DDL和DML和DQL的基本语法

Vite3 learning records

Connect with Hunan Ca and use U_ Key login

深入浅出边缘云 | 1. 概述
现在完全不知道怎么同步

Leetcode hot topic hot52-100
![[wechat applet] do you know about applet development?](/img/3d/da58255aeb6bf6bc5021d988906bcc.png)
[wechat applet] do you know about applet development?

WinDbg practice -- Introduction

ssm+mysql实现零食商城系统(电商购物)
随机推荐
Network learning infrared module, 8-way emission independent control
[attack and defense world web] difficulty four-star 12 point advanced question: flatscience
Jetson nano烧录踩坑记(一定可以解决你的问题)
高数下|二重积分的计算2|高数叔|手写笔记
High numbers | calculation of triple integral 1 | high numbers | handwritten notes
比较关注证券公司究竟哪个佣金最低?请问网上开户安全么?
Day 12: continued day 11 (BGP related knowledge)
Edge cloud | 1. overview
Kubevela offline installation
LU_ASR01语音模块使用
一道golang中关于for range常见坑的面试题
【微信小程序】你了解小程序开发吗?
【持续更新】树莓派启动与故障系列集锦
Install under win7-vs2012 Net framework work
Interpretation of Flink catalog
做一个有职业操守的软件匠人
1309_STM32F103上增加GPIO的翻转并用FreeRTOS调度测试
[kernel] platform bus model for driving development and learning
Educational Codeforces Round 132 A-D题解
Comment présenter votre expérience de projet lors d'une entrevue