Android发送广播的过程

代码实现

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.btn_Broadcast);//获取广播按钮
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction("马云");
//为Itent添加动作zuckerg
sendBroadcast(intent);//发送广播
}
});
}
}

MyReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast; public class MyReceiver extends BroadcastReceiver {
private static final String ACTIONI = "马云";//动作1
private static final String ACTION2 = "小腾腾";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(ACTIONI)){//回复第一个广播
Toast.makeText(context,
"MyReceiver收到:马云的广播",
Toast.LENGTH_SHORT).show();
}else if(intent.getAction().equals(ACTION2)){//回复第二个广播
Toast.makeText(context,"MyReceiver收到:小腾腾的广播",
Toast.LENGTH_SHORT).
show();
}
}
}

AndroidManifest.xml清单文件注册BroadcastReceiver

        <!--
注册广播接收器
android:enabled="true"可以被实例化
android:exported="true"能接收其他app的广播
-->
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
tools:ignore="WrongManifestParent">
<intent-filter>
<!--配置允许接收的Action-->
<action android:name="马云"/>
<action android:name="小腾腾"/>
</intent-filter>
</receiver>
</application>

Android学习笔记BroadcastReceiver(广播接收者)的更多相关文章

  1. Android 学习笔记 BroadcastReceiver广播...

    PS:不断提升自己,是件好事... 学习内容: 1.BroadcastReceiver的使用.. 2.通过BroadcastReceiver去启动Service... 1.BroadcastRecei ...

  2. Android学习笔记_19_广播接收者 BroadcastReceiver及其应用_窃听短信_拦截外拨电话

    一.广播接收者类型: 广播被分为两种不同的类型:“普通广播(Normal broadcasts)”和“有序广播(Ordered broadcasts)”. 普通广播是完全异步的,可以在同一时刻(逻辑上 ...

  3. Android学习笔记(广播机制)

    1.Android的广播机制介绍 收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播电台播放的内容都不相同.接受广播时广播(发送方)并不在意我们(接收方)接收到广播时如何处理.好比我们收听交 ...

  4. 【转】Pro Android学习笔记(九八):BroadcastReceiver(2):接收器触发通知

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.sina.com.cn/flowingflying或作者@恺风Wei-傻瓜与非傻瓜 广播接 ...

  5. 【转】 Pro Android学习笔记(七五):HTTP服务(9):DownloadManager

    目录(?)[-] 小例子 保存在哪里下载文件信息设置和读取 查看下载状态和取消下载 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载须注明出处:http://blog.csd ...

  6. 九、Android学习第八天——广播机制与WIFI网络操作(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 九.Android学习第八天——广播机制与WIFI网络操作 今天熟悉了An ...

  7. 【转】 Pro Android学习笔记(八八):了解Handler(2):什么是Handler

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 之前我们有一篇很好的博文<Andro ...

  8. 【转】Pro Android学习笔记(十二):了解Intent(下)

    解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...

  9. 【转】Pro Android学习笔记(十):了解Intent(上)

    目录(?)[-] Intent基本含义 系统的Intent Android引入了Intent的概念来唤起components,component包括:1.Activity(UI元件) 2.Servic ...

随机推荐

  1. Fundamental ES6 Part-I

    Exercise-01 with Solution Write a JavaScript program to compare two objects to determine if the firs ...

  2. JS中的bind方法

    # bind的机制 ``` var foo = function(){} var bar = foo; console.log(foo === bar) //true /--------------- ...

  3. js 获取百度搜索关键词的代码

    有可能有时候我们会用到在百度搜什么关键词进来我们的网站的,所有我们又想拿到用户搜索的关键词. 这是我研究了半天所得出的办法.话不多说直接贴代码 <script> function quer ...

  4. shiro认证通过之后的授权

    subject.hasRole("") ; subject.hasRoles(List); subject.hasAllRoles(); subject.isPermitted(& ...

  5. [Objective-C] 007_Foundation框架之NSString与NSMutableString

    在Cocoa Foundation中的NSString和NSMutableString类,为我们提供了Unicode字符串的支持,NSString和NSMutableString类最大的区别是:NSS ...

  6. Spring_配置Bean & 属性配置细节

    1.Spring容器 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.Sp ...

  7. PAT1067 试密码 (20分)——测试点4分析 一个易错点

    1067 试密码 (20分)   当你试图登录某个系统却忘了密码时,系统一般只会允许你尝试有限多次,当超出允许次数时,账号就会被锁死.本题就请你实现这个小功能. 输入格式: 输入在第一行给出一个密码( ...

  8. 概念辨析-Hardware Description还是Hardware Developing?

    https://mp.weixin.qq.com/s/j4Ndo1R4Go9IaGbhE_nsTg   Verilog, standardized as IEEE 1364, is a hardwar ...

  9. SpringMVC(一)概述、解析器与注解

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.SpringMVC的概述 1.概述 Spring MVC框架是一个开源的Java平台,为开发强大的基 ...

  10. 数据库之 MySQL --- 数据处理 之多表查询 (三)

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.多表查询 [1]什么是多表查询? 即,从多个表中获取数据. 注意: 在多表查询是,如果列明在两个表中 ...