首先看一下什么是 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. artTemplate模板引擎

    artTemplate模板引擎         <li>索引 {{i + 1}} :{{value}}</li>     {{/each}} </ul> </ ...

  2. Mysql常用数据类型

    Mysql常用数据类型 数字: 字符串: 时间:

  3. 11-JS基础

    JS声明变量 变量必须以字母开头 变量也能以 $ 和 _ 符号开头(不推荐这么做) 变量名称对大小写敏感(y 和 Y 是不同的变量) **提示:JavaScript 语句和 JavaScript 变量 ...

  4. [问题2014S12] 解答

    [问题2014S12]  解答 先证明一个简单的引理. 引理  设 \(B\) 为 \(n\) 阶半正定 Hermite 阵, \(\alpha\) 为 \(n\) 维复列向量, 若 \(\overl ...

  5. 微信小程序文件作用域模块引用

    文件作用域 在 JavaScript 文件中声明的变量和函数只在该文件中有效:不同的文件中可以声明相同名字的变量和函数,不会互相影响. 通过全局函数 getApp() 可以获取全局的应用实例,如果需要 ...

  6. [Python爬虫]cnblogs博客备份工具(可扩展成并行)

    并发爬虫小练习. 直接粘贴到本地,命名为.py文件即可运行,运行时的参数为你想要爬取的用户.默认是本博客. 输出是以用户名命名的目录,目录内便是博客内容. 仅供学习python的多线程编程方法,后续会 ...

  7. Cannot find class for bean with name '/hello' defined in ServletContext resource

    Cannot find class for bean with name '/hello' defined in ServletContext resource [/WEB-INF/chapter2- ...

  8. SQL Server 查询表的记录数(3种方法,推荐第一种)

    http://blog.csdn.net/smahorse/article/details/8156483 --SQL Server 查询表的记录数 --one: 使用系统表. SELECT obje ...

  9. Redis哈希表的实现要点

    Redis哈希表的实现要点 哈希算法的选择 针对不同的key使用不同的hash算法,如对整型.字符串以及大小写敏感的字符串分别使用不同的hash算法: 整型的Hash算法使用的是Thomas Wang ...

  10. 使用 Jasmine 进行测试驱动的 JavaScript 开发

    Jasmine 为 JavaScript 提供了 TDD (测试驱动开发)的框架,对于前端软件开发提供了良好的质量保证,这里对 Jasmine 的配置和使用做一个说明. 目前,Jasmine 的最新版 ...