首先看一下什么是 BroadcastReceiver

BroadcastReceiver:直译是“广播接收者”,所以它的作用是用来接收发送过来的广播的。

那我们有必要知道:什么是广播。广播,我的理解就是系统中消息的一种变种;就是当一个事件发生时,比如,系统突然断网,系统就发一个广播消息给所有的接收者,所有的接收者在得到这个消息之后,就知道,啊哦,现在没网络了,我的程序应该怎么办,比如显示默认图片、提示用户等。前面,我们说了,BroadcastReceiver就是一个广播消息接收者。

另外我还要提一下,广播之间信息的传递是通过Intent对象来传递的;在《详解Intent》系列文章中,我讲了,Intent调用分为显示调用的隐式调用两种,由于这里能通知到所有的接收者,所以肯定不能利用显示调用,只有利用隐式调用Intent对象了。(这里的隐式调用,并不是真正意义上的Intent隐式调用,因为Intent隐式调用,当出现很多匹配应用时,会以列表形式提示用户选择一个启动,而这里不同的地方在于,当有很多匹配项时,会给所有的匹配项都发一个消息,我说隐式调用,只是方便大家理解构造Intent的方法,即必须利用构造隐式Intent的方法来构造)

1,创建一个空项目,然后new一个新的BroadcastReceiver(new--->other)MyReceiver.java

 public class MyReceiver extends BroadcastReceiver {

     //用于隐式调用与注册
public static final String ACTION = "examples.ouc.com.broadcastreceiver.intent.action.MyReceiver";
public MyReceiver() {
} //监控广播操作是否完成
@Override
public void onReceive(Context context, Intent intent) { //通过intent传递信息
System.out.println("Receive news, and the news is :" + intent.getStringExtra("data"));
}
}

MyReceiver.java

需要把AndroidManefest中的这句话去掉,否则它是默认一直绑定的

<receiver
android:name=".MyReceiver11"
android:enabled="true"
android:exported="true"></receiver>

2,然后在页面中添加几个按钮,用于绑定,解除绑定,和发送

<Button
android:id="@+id/btnSendMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送消息" /> <Button
android:id="@+id/btnReg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册接收器" /> <Button
android:id="@+id/btnUnreg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注销接收器" />

activity_main

3,配置broadcast服务

 public class MainActivity extends AppCompatActivity implements View.OnClickListener {

     @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.btnSendMessage).setOnClickListener(this);
findViewById(R.id.btnReg).setOnClickListener(this);
findViewById(R.id.btnUnreg).setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnSendMessage:
//Intent i = new Intent(this,MyReceiver.class);
//必须采用隐式的
Intent i = new Intent(MyReceiver.ACTION);
i.putExtra("data","iCyhuSky");
sendBroadcast(i);
break; case R.id.btnReg:
//如果没有绑定,就开启
if (receiver== null){
receiver = new MyReceiver();
registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));
}
break; case R.id.btnUnreg:
//如果开启了,就关闭
if (receiver!=null){
unregisterReceiver(receiver);
receiver = null;
}
break; }
} //标志服务是否绑定
private MyReceiver receiver = null;
}

MainActivity

4,然后可发布运行了

开始时,点击“发送消息”,后台logcat没有输出

点击“注册接收器”,然后点击“发送消息”,后台logcat会输出我们传递的那句话

点击“注销接收器”,然后点击“发送消息”,后台logcat就没有输出了!

5,优先级问题:

(1)默认状态下:

如果两个receiver指明到同一个action,那么后创建的优先级比较高,先执行代码

(2)也可我们人工代码修改优先级

第一个接收器:

 <receiver android:name=".MyReceiver1">
<intent-filter android:priority="9">
<action android:name="......"
</intent-filter>
</receiver>

第二个接收器:

 <receiver android:name=".MyReceiver2">
<intent-filter android:priority="9">
<action android:name="......"
</intent-filter>
</receiver>

从这里我们可以发现,二者除了名字不同外,只有priority有区别,值比较大的优先执行.

priority汉语就是优先级的意思。。。。

(3)当我们在优先级比较高的接收器中添加这样一句时(红色),其他的将不再执行:

 public void onReceive(Context context, Intent intent) {

         //通过intent传递信息
System.out.println("Receive news, and the news is :" + intent.getStringExtra("data"));
abortBroadcast();
}

如果只是这样会报错,需要修改MainActivity中的发生发送按钮

 public void onClick(View v) {
switch (v.getId()){
case R.id.btnSendMessage:
//Intent i = new Intent(this,MyReceiver.class);
//必须采用隐式的
Intent i = new Intent(MyReceiver.ACTION);
i.putExtra("data","iCyhuSky");
// sendBroadcast(i);
sendOrderedBroadcast(i,null);
break;

android广播接收器BroadcastReceiver的更多相关文章

  1. (八)Android广播接收器BroadcastReceiver

    一.使用Broadcast Reciver 1.右击java文件夹,new->other->Broadcast Receiver后会在AndroidManifest.xml文件中生成一个r ...

  2. android广播接收器

    Android程序创建广播接收器继承BroadcastReceiver Android广播接收器需要在AndroidManifest.xml文件中声明: <recevie android:nam ...

  3. android在广播接收器BroadcastReceiver里面再进行发送广播,造成当前广播接收器不断循环执行问题

    最近在公司处理项目时,用到锁屏状态弹出activity进行提示,类似QQ消息弹屏提示的功能.当中用到了,假如该弹出activity已经位于锁屏界面外时,将不进行再次弹窗,而是发送广播进行通知数据更新, ...

  4. Xamarin.Android广播接收器与绑定服务

    一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架起一座桥梁,通过本节的学习,你将会学到广播与绑定服务 ...

  5. Android - 广播接收者 - BroadcastReceiver

    BroadcastReceiver 介绍: 广播是一种广泛运用的在应用程序之间传输信息的机制 .而 BroadcastReceiver 是对发送出来的广播 进行过滤接收并响应的一类组件 接受一种或者多 ...

  6. Android广播接收器Broadcast Receiver-android学习之旅(十二)

    首先继承BroadcastReceiver类,并在manifest中注册 public class MyReceiver extends BroadcastReceiver { public MyRe ...

  7. Android广播接收器和Activity间传递数据

    Activity向广播接收器传递数据很简单,只需要在发送广播前将数据put进Intent中就行了. 广播接收器怎么向Activity传送数据?这里要用到接口,通过在广播接收器里定义一个接口,然后让接收 ...

  8. Android广播接收器里弹出对话框

    不多说,直接上车... public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(fina ...

  9. Android广播接收器BroadcastRceiver

    一.使用BroadcastRceiver 1.创建BroadcastRceiver(MyRceiver),重写OnReceiver: public void onReceive(Context con ...

随机推荐

  1. String s ; 和 String s = null ; 和 String s = "" ; 的却别

    String s ;该语句表示只是声明了一个引用变量,但是并没有初始化引用,所以对变量s的任何操作(除了初始化赋值外) 都将引发异常. String s=null; 表示未申请任何内存资源,即此语句表 ...

  2. jquery判断id是否存在

    1.判断标签是否存在 ){ 存在 } 2.判断(id="id名"的标签)是否存在,下面的不可以!!!因为 $("#id") 不管对象是否存在都会返回 objec ...

  3. 安装Ifconfig

    1.ifconfig 2.whereis 检查 3.yum search ifconfig 4.分割线下面让我们安装 net-tools.x86_64 执行 yum -y install net-to ...

  4. 【java报错】Could not instantiate listener

    这个错误以前出现过好几次,莫名其妙的出现,莫名其妙的解决掉...昨天好好的,今天又出现了,记下来 2015-03-03 09:38:45.105:INFO:oejs.Server:jetty-8.1. ...

  5. 网页设计中常用的19个Web安全字体

    来自http://www.jb51.net 在Web编码中,CSS默认应用的Web字体是有限的,虽然在新版本的CSS3,我们可以通过新增的@font-face属性来引入特殊的浏览器加载字体.但多数情况 ...

  6. Animating graphic objects in Windows Forms.

    原文: Animating graphic objects in Windows Forms. http://bobpowell.net/animation.aspx 文件下载备份:http://fi ...

  7. 关于 Oracle 的数据导入导出及 Sql Loader (sqlldr) 的用法

    在 Oracle 数据库中,我们通常在不同数据库的表间记录进行复制或迁移时会用以下几种方法: 1. A 表的记录导出为一条条分号隔开的 insert 语句,然后执行插入到 B 表中2. 建立数据库间的 ...

  8. (转) C/C++中const关键字详解

    文章转自  http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777416.html 为什么使用const?采用符号常量写出的代码更容易维 ...

  9. 使用Sonatype Nexus搭建Maven私服后如何添加第三方JAR包?

    Sonatype Nexus简介 登录Nexus后,点击右侧的“Repositories”,显示当前Nexus所管理的Repository: 默认情况下Nexus为我们创建了以下主要的Reposito ...

  10. shell zsh

    之前用fish安装homebrew成功了 但是忘记怎么安装的了 以后要纪录下来了 设置zsh为默认的 shell https://github.com/robbyrussell/oh-my-zsh/w ...