Broadcast发送广播
一、知识介绍
1、【广播分类】
①有序广播:接收者A收到广播传递给B,B传给C,有序传递。任何一个环节都可以终止广播,也可以修改广播中携带的数据。
发送的方式:sendOrderedBroadcast(intent,receiverPermission);
【提示】①第二个参数是设置发送的权限,这里可以设为null
②接收有序广播是需要在intent-flter中设置priority,值越大则先执行,相同则按照注册顺序
②无序广播:一个广播发送者,向所有接收者同时发送广播,也就是ABC接收者都同时响应。
发送方式:sendBroadcast(intent)
2、【广播接收者】按是否常驻分类
①常驻型广播接收者:在androidManifest.xml中注册,只要应用程序没有被卸载就持续存在。
②非常驻型广播接收者:在java代码中注册,一般随Activity或者Service组件产生而产生,随他们销毁而销毁。生命周期比较短。使用的方法是registerReceiver(参数1:广播接收者实例,参数2:频道(意图过滤器));unregisterReceiver(广播接收者实例)
二、项目一【发送广播】
【步骤】
①定义一个广播接收者,自定义添加intent-fliter中的action name
②添加按钮,点击事件
③定义intent,设置action,发送广播
【项目结构】

【MyReceiver】
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { @Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
Toast.makeText(context, "收到广播", Toast.LENGTH_SHORT).show();
}
}
【AndroidManifest.xml】
<receiver
android:name=".receiver.MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.MyApplication2.myreceiver" />
</intent-filter>
</receiver>
【activity_main.xml】
<Button
android:id="@+id/btn"
android:text="发送广播"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
【MainActivity】
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity { Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btn = findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.MyApplication2.myreceiver");
sendBroadcast(intent);
}
}); }
}
【提示】发送广播intent设置的action要和广播接受者设置的action相同,这样广播接收者才能收到发送的广播
【效果】点击

二、项目二【发送有序广播】
【步骤】
①定义三个广播接收者,观察顺序
②添加按钮点击
③设置intent,发送有序广播
【项目结构】

【定义三个广播接收者并注册】
<receiver
android:name=".receiver.MyOrderReceiver1"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="com.example.MyApplication2.myreceiver" />
</intent-filter>
</receiver>
<receiver
android:name=".receiver.MyOrderReceiver2"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="100">
<action android:name="com.example.MyApplication2.myreceiver" />
</intent-filter>
</receiver>
<receiver
android:name=".receiver.MyOrderReceiver3"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="10">
<action android:name="com.example.MyApplication2.myreceiver" />
</intent-filter>
</receiver>
【提示】设置priority为不同的值,action name为相同的,接收同一个广播



【MainActivity】
btn2 = findViewById(R.id.btn2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.MyApplication2.myreceiver");
sendOrderedBroadcast(intent,null);
}
});
【效果】
点击按钮执行顺序



Broadcast发送广播的更多相关文章
- 在命令行中通过adb shell am broadcast发送广播通知
通过命令行执行adb shell am broadcast发送广播通知. adb shell am broadcast 后面的参数有:[-a <ACTION>][-d <DATA_U ...
- Android通过adb shell am broadcast发送广播 参数说明
通过命令行执行adb shell am broadcast发送广播通知. adb shell am broadcast 后面的参数有: <INTENT> specifications in ...
- [AX2012]发送广播邮件
AX 2012可以使用MAPI或者SMTP发送邮件,MAPI是客户端方法,需要outlook的协作,而SMTP则是服务器端方法,要求SMTP允许AOS服务器通过它中继.这里要讲的就是如何通过SMTP发 ...
- 通过 adb命令发送广播
我们经常用到模块设备发送广播,此处记录一下: 首先进入adb 使用命令: adb shell 发送广播 例: am broadcast -a action.com.custom.broadcast.q ...
- Android应用程序发送广播(sendBroadcast)的过程分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6744448 前面我们分析了Android应用程 ...
- linux中C语言发送广播报文
2. 指令的解决方法: oute add -net 255.255.255.255 netmask 255.255.255.255 dev eth0 metric 1 或者 route add -ho ...
- Android 两种注册、发送广播的区别
前言:前面文章记录了Service的使用,这次来记录另一个四个组件之一的BroadcastReceiver.主要介绍两种发送和注册广播的区别. BroadcastReceiver广播接收者用于接收系统 ...
- Ordered Broadcast有序广播
sendBroadcast()发生无序广播 sendOrderedBroadcast()发送有序广播 activity_main.xml <LinearLayout xmlns:android= ...
- Angular发送广播和接收广播
home.module.ts import {BroadcastService} from "../broadcast.service"; @NgModule({ imports: ...
随机推荐
- Android 实现卡片翻转的动画(翻牌动画)
Android 实现卡片翻转的动画(翻牌动画) 需求描述 点击卡片,卡片翻转过来显示内容. 点击左边的卡片,将卡片翻转显示右边的图片结果. 功能实现 因为要翻转所以使用动画来完成翻转的动画.动画分为两 ...
- [Swift]LeetCode189. 旋转数组 | Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: ...
- [Swift]LeetCode375. 猜数字大小 II | Guess Number Higher or Lower II
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- [Swift]LeetCode452. 用最少数量的箭引爆气球 | Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Windows系统下安装Redis
1.首先你要有redis-latest-windws和redisclient-客户端工具 2.在redis-latest-windws文件夹内创建一个批处理文件 start.bat 创建批处理文件的 ...
- Python面向对象中的类和对象
类和对象 目标 类和对象的概念 类和对象的关系 类的设计 01. 类和对象的概念 类 和 对象 是 面向对象编程的 两个 核心概念 1.1 类 类 是对一群具有 相同 特征 或者 行为 的事物的一个统 ...
- 网络协议 2 - IP 是怎么来,又是怎么没的?
了解完网络协议,我们会发现,网络通信的五层模型里,有两个很重要的概念:IP 地址和 MAC 地址. 那么 IP 地址是怎么来的,又是怎么没的?MAC 地址与 IP 地址又有什么区别? 这回答上面问题前 ...
- 死磕 java集合之TreeMap源码分析(三)- 内含红黑树分析全过程
欢迎关注我的公众号"彤哥读源码",查看更多源码系列文章, 与彤哥一起畅游源码的海洋. 删除元素 删除元素本身比较简单,就是采用二叉树的删除规则. (1)如果删除的位置有两个叶子节点 ...
- Jquery.ajax dataType参数
dataType 类型:String 预期服务器返回的数据类型.如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息来智能判断,比如 XML MIME 类型就被识别为 XML.在 1.4 ...