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. OpenStack的Heat组件详解

    一:简介     一.什么Heat 1. Heat 是一套业务流程平台,旨在帮助用户更轻松地配置以 OpenStack 为基础的云体系.利用Heat应用程序,开发人员能够在程序中使用模板以实现资源的自 ...

  2. 关于mantisBT2.22安装插件Inline column configuration 2.0.0时提示缺少依赖jQuery UI Library 1.8

    1.首先直接下载最新的那个1.12即可 https://github.com/mantisbt-plugins/jQuery-UI/releases 2.上传到mantis的插件目录下:……manti ...

  3. Java——native关键字

    说明:在使用HashSet的过程中,查看Object.java过程中发现hashCode()方法是以native关键字修饰,没看到过该关键字,这里记录下来. native关键字用来修饰方法,是使用一些 ...

  4. [工具-001]C++更换EXE的ICON图标

    我们都知道每个可执行文件EXE都会有自己的图标,它可以在项目生成的时候进行指认,但是有时候我们会遇到两种情况:1.没有源代码,2.我们的项目很多,一个个进行更换很耗时.本人就是因为接到这么一个需求,要 ...

  5. Java IO(十六)InputStreamReader 和 InputStreamWriter

    Java IO(十六)InputStreamReader 和 InputStreamWriter 一.介绍 InputStreamReader 和 OutputStreamWriter 是字节流通向字 ...

  6. Pytorch分类和准确性评估--基于FashionMNIST数据集

    最近在学习Pytorch v1.3最新版和Tensorflow2.0. 我学习Pytorch的主要途径:莫烦Python和Pytorch 1.3官方文档 ,Pytorch v1.3跟之前的Pytorc ...

  7. centos系统与ubuntu系统的区别

    centos和ubuntu简述 CentOS(Community ENTerprise Operating System)是Linux发行版之一,它是来自于Red Hat Enterprise Lin ...

  8. axios发送post form请求

    axios发送post form请求 只需修改url和data即可 axios({ url: '/user', method: 'post', data: { firstName: 'Fred', l ...

  9. Rocket - devices - PLIC

    https://mp.weixin.qq.com/s/FR3yeLLBqy0n-fflw-ATgg 简单介绍TLPLIC的实现. 1. GatewayPLICIO PLIC是Platform-Leve ...

  10. Java并发编程 (十) 多线程并发拓展

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.死锁 1.死锁的定义 所谓的死锁是指两个或两个以上的线程在等待执行的过程中,因为竞争资源而造成的一种 ...