Android广播机制的基本使用
一提到广播我们第一感觉就会联想到小时候村里面的广播,安卓的广播机制也是类似于大喇叭。有发送广播的地方,也有接收广播的地方。但是具体怎么操作呢,我们来一步一步的看下去~
安卓的广播种类
- 系统发送的广播:具体有哪些可以看下这篇Blog
http://blog.sina.com.cn/s/blog_7dbac1250101mt5h.html - app发送的广播:这个是我们自定义的,需要在AndroidMainFest.xml文件中出册,在发送广播的时候也要用到
如何发送广播
- 使用意图Intent发送
1.创建好Intent发送对象,构造方法如下
//参数是需要发送的信息,一般是字符串
public static final String ACTION_INTENT_TEST = "com.tao.broadCastReceive";
Intent intent = new Intent(ACTION_INTENT_TEST);
```
2.使用Activity中的一个方法进行发送广播
+ 同步广播
同步广播可以几乎再一瞬间到达所有的广播接收站,程序对此无法控制,不能停止,不能阶段
```java
//此方法是Context中的方法,同步广播
sendBroadcast(intent);
- 有序广播
广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,在AndroidManifest.xml的设置;
比如存在3个广播接收者A、B、C,优先级A>B>C,因此A最先收到广播,当A收到广播后,可以向广播中可以: - 添加一些数据给下一个接收者(intent.putExtra())
- 终止广播(abortBroadcast());
sendOrderedBroadcast(intent,null);
广播的接收
- 构建一个类,此类继承BroadcastReceiver,并实现onReceive方法,但是记住,onReceive的代码不宜过长。不适合执行长时间操作的代码,如I/O,网络请求,文件读取的等,否则会出现吧ANR(Application No Response),应当创建并启动一个Services并启动,再服务中执行操作(记住服务是工作在UI线程中的哦!!!)
public class Receiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
...
}
}
- 再AndroidMainFest.xml文件中注册广播
<receiver android:name=".Receiver">
<intent-filter android:priority="1000">
<action android:name="com.tao.broadCastReceive"/>
</intent-filter>
</receiver>
这样就完成一个广播的发送了,当然如果需要接收系统的广播,则可以在AndroidMainFest文件中进行设置Action属性,则就可以接收系统发出的广播信息。
实例代码
- MainActivity.java
public class MainActivity extends AppCompatActivity {
private Button sendBroadCastReceive;
private EditText titleContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendBroadCastReceive= (Button) findViewById(R.id.sendBroadCastReceive);
titleContent= (EditText) findViewById(R.id.titleContent);
sendBroadCastReceive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent sendBroadCast=new Intent();
sendBroadCast.setAction("coom.tao.selfBroadCastReceive");
sendBroadCast.putExtra("title",titleContent.getText().toString().trim());
sendBroadcast(sendBroadCast);
}
});
}
}
- activity_main.xml 布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tao.boedercast.MainActivity">
<EditText
android:id="@+id/titleContent"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
android:layout_marginBottom="10dp"
android:layout_above="@+id/sendBroadCastReceive"
android:hint="请输入发送的内容"
/>
<Button
android:id="@+id/sendBroadCastReceive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="发送广播"
/>
</RelativeLayout>
预览

- 自定义接收BroadcastReceive
public class MyBroadCastReceive extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String titleContent=intent.getStringExtra("title");
Toast.makeText(context,titleContent,Toast.LENGTH_LONG).show();
}
}
- androidmainfest.xml文件注册
<receiver android:name=".MyBroadCastReceive">
<intent-filter android:priority="1000">
<action android:name="coom.tao.selfBroadCastReceive"></action>
</intent-filter>
</receiver>
一个简单的自定义广播完成了,欢迎大家指正批评,谢谢!
本博客内容一致同步到本人的简书博客:http://www.jianshu.com/p/c01397361ba0 欢迎访问留言交流
Android广播机制的基本使用的更多相关文章
- Android随笔之——Android广播机制Broadcast详解
在Android中,有一些操作完成以后,会发送广播,比如说发出一条短信,或打出一个电话,如果某个程序接收了这个广播,就会做相应的处理.这个广播跟我们传统意义中的电台广播有些相似之处.之所以叫做广播,就 ...
- Android广播机制的深入学习
部分内容转载自http://www.cnblogs.com/lwbqqyumidi/p/4168017.html 1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者 ...
- Android总结篇系列:Android广播机制
1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...
- Android广播机制概述
1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...
- Android广播机制:Broadcast
转载:Android总结篇系列:Android广播机制 1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广 ...
- Android(java)学习笔记172:BroadcastReceiver之 Android广播机制
Android广播机制 android系统中有各式各样的广播,各种广播在Android系统中运行,当"系统/应用"程序运行时便会向Android注册各种广播.Android接收到广 ...
- Android广播机制(转)
1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的就是广播接收者(广播接收器).广播作为Android组件间的通 ...
- Android广播机制
原文出处: Android总结篇系列:Android广播机制 1.Android广播机制概述 Android广播分为两个方面:广播发送者和广播接收者,通常情况下,BroadcastReceiver指的 ...
- Android(java)学习笔记115:BroadcastReceiver之 Android广播机制
Android广播机制 android系统中有各式各样的广播,各种广播在Android系统中运行,当"系统/应用"程序运行时便会向Android注册各种广播.Android接收到广 ...
- Android广播机制简介
为什么说Android中的广播机制更加灵活呢?这是因为Android中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容,这些广播可能是来自于系统的,也可能是来 ...
随机推荐
- 使用C语言实现一个自动刷弹幕的程序
本文使用两种方式来进行刷弹幕操作 1 模拟键盘输入,自动输入文字,然后点击回车. 2 操作剪切板,直接将剪切板的文字粘贴到输入框,然后回车. 模拟键盘输入 如果要输入"弹幕"这两个 ...
- .Net RabbitMQ系列之环境搭建于RabbitMQ基本介绍
本系列主要讲解RabbitMQ在.Net环境下的应用,由于Linux环境下,本人Linux功力有限,所以本系列的RabbitMQ跑在Windows环境中.所以的配置之类都在Windows环境中进行. ...
- mysql 开发进阶篇系列 19 MySQL Server(innodb_flush_log_at_trx_commit与sync_binlog)
一. innodb_flush_log_at_trx_commit 这个参数名称有个log,一看就是与日志有关.是指:用来控制缓冲区(log buffer)中的数据写入到日志文件(log file), ...
- MySQL批量插入数据的几种方法
最近公司要求测试数据库的性能,就上网查了一些批量插入数据的代码,发现有好几种不同的用法,插入同样数据的耗时也有区别 别的先不说,先上一段代码与君共享 方法一: package com.bigdata; ...
- [NewLife.XCode]增删改查入门
NewLife.XCode是一个有10多年历史的开源数据中间件,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和运行日志来进行深入分析,蕴含 ...
- ES6躬行记(3)——解构
解构(destructuring)是一种赋值语法,可从数组中提取元素或从对象中提取属性,将其值赋给对应的变量或另一个对象的属性.解构地目的是简化提取数据的过程,增强代码的可读性.有两种解构语法,分别是 ...
- api网关揭秘--spring cloud gateway源码解析
要想了解spring cloud gateway的源码,要熟悉spring webflux,我的上篇文章介绍了spring webflux. 1.gateway 和zuul对比 I am the au ...
- springboot情操陶冶-SpringApplication(一)
SpringApplication是所有springboot的入口类,分析此类有助于我们了解springboot的工作机制.本文以2.0.3.REALEASE版本作分析 SpringApplicati ...
- centos7安装kafka_2.11
1.下载 官网地址:http://kafka.apache.org/downloads.html 下载:wget https://www.apache.org/dyn/closer.cgi?path= ...
- 实战!基于lamp安装wordpress详解-技术流ken
简介 LAMP 是Linux Apache MySQL PHP的简写,其实就是把Apache, MySQL以及PHP安装在Linux系统上,组成一个环境来运行动态的脚本文件.现在基于lamp搭建wor ...