一、知识介绍

  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发送广播的更多相关文章

  1. 在命令行中通过adb shell am broadcast发送广播通知

    通过命令行执行adb shell am broadcast发送广播通知. adb shell am broadcast 后面的参数有:[-a <ACTION>][-d <DATA_U ...

  2. Android通过adb shell am broadcast发送广播 参数说明

    通过命令行执行adb shell am broadcast发送广播通知. adb shell am broadcast 后面的参数有: <INTENT> specifications in ...

  3. [AX2012]发送广播邮件

    AX 2012可以使用MAPI或者SMTP发送邮件,MAPI是客户端方法,需要outlook的协作,而SMTP则是服务器端方法,要求SMTP允许AOS服务器通过它中继.这里要讲的就是如何通过SMTP发 ...

  4. 通过 adb命令发送广播

    我们经常用到模块设备发送广播,此处记录一下: 首先进入adb 使用命令: adb shell 发送广播 例: am broadcast -a action.com.custom.broadcast.q ...

  5. Android应用程序发送广播(sendBroadcast)的过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6744448 前面我们分析了Android应用程 ...

  6. linux中C语言发送广播报文

    2. 指令的解决方法: oute add -net 255.255.255.255 netmask 255.255.255.255 dev eth0 metric 1 或者 route add -ho ...

  7. Android 两种注册、发送广播的区别

    前言:前面文章记录了Service的使用,这次来记录另一个四个组件之一的BroadcastReceiver.主要介绍两种发送和注册广播的区别. BroadcastReceiver广播接收者用于接收系统 ...

  8. Ordered Broadcast有序广播

    sendBroadcast()发生无序广播 sendOrderedBroadcast()发送有序广播 activity_main.xml <LinearLayout xmlns:android= ...

  9. Angular发送广播和接收广播

    home.module.ts import {BroadcastService} from "../broadcast.service"; @NgModule({ imports: ...

随机推荐

  1. 字符串----hiho字符串(尺取法)

    注意:这道题的解法和最短摘要一样,都是采用尺取法解决问题,注意这儿题目要求恰好包含,也就是说这个hiho字符串必须包含2个'h'.1个'i'和1个'o'.一个不能多,一个也不能少. import ja ...

  2. [Swift]LeetCode42. 接雨水 | Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  3. [Swift]LeetCode161. 一次编辑距离 $ One Edit Distance

    Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串S和T,确定它们是否都是是一 ...

  4. [Swift]LeetCode893. 特殊等价字符串组 | Groups of Special-Equivalent Strings

    You are given an array A of strings. Two strings S and T are special-equivalent if after any number ...

  5. Socket.io发送消息含义

    仅作收藏:转自博客园 若相忆; // send to current request socket client socket.emit('message', "this is a test ...

  6. DOM事件第一弹

    近期温习了部分w3c上关于DOM事件的规范,发现以前有些模糊的概念更加清晰,以及受到罗胖(罗辑思维)的影响,很是想分享自己的了解的东西,希望大家给予指正或补充. 一.事件类型 参数 事件接口 初始化方 ...

  7. asp.net core 系列 5 MVC框架路由(上)

    一. 概述 介绍asp.net core路由时,我初步想了下,分几篇来说明.  路由的知识点很多,参考了官方文档提取出一些重要的知识点来说.    在ASP.NET Core中是使用路由中间件来匹配传 ...

  8. ADO.NET中COMMAND对象的ExecuteNonQuery、ExcuteReader和ExecuteScalar方法

    1.ExecuteNonQuery方法.该方法执行更新操作,即与UPDATE.INSERT.DELETE等语句有关的操作,在这种情况下,返回值是命令影响的行数.对其他语句,如SET或CREATE,则返 ...

  9. 使用mpvue开发小程序教程(一)

    前段时间,美团开源了mpvue这个项目,使得我们又多了一种用来开发小程序的框架选项.由于mpvue框架是完全基于Vue框架的(重写了其runtime和compiler),因此在用法上面是高度和Vue一 ...

  10. SpringCloud入门之常用的配置文件 application.yml和 bootstrap.yml区别

    作者其他技术文章 1) Spring Boot 简介 2)SpringCloud入门之YAML格式文件规范学习 3)SpringCloud入门之Spring Boot多环境配置切换指南 4) Elas ...