首先看一下什么是 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. [转]iOS学习之UINavigationController详解与使用(三)ToolBar

    转载地址:http://blog.csdn.net/totogo2010/article/details/7682641 iOS学习之UINavigationController详解与使用(二)页面切 ...

  2. asp.net 之

    <script type="text/javascript"> //获取客户端实例 var pa = Sys.WebForms.PageRequestManager.g ...

  3. IE/Firefox/Chrome等浏览器保存Cookie的位置

    IE/Firefox/Chrome等浏览器保存Cookie的位置 原文  http://smilejay.com/2013/04/browser-cookie-location/   前面写了篇长文( ...

  4. Sprint(第二天11.15)

    Sprint1第一阶段 1.类名:软件工程-第一阶段 2.时间:11.14-11.23 3.选题内容:点餐系统 4.团队博客地址:http://www.cnblogs.com/iamCarson/ 团 ...

  5. 学习mongo系列(十)MongoDB 备份(mongodump)与恢复(mongorerstore) 监控(mongostat mongotop)

    一.备份 在Mongodb中我们使用mongodump命令来备份MongoDB数据.该命令可以导出所有数据到指定目录中. mongodump命令可以通过参数指定导出的数据量级转存的服务器. mongo ...

  6. Java文件操作与输入输出流

    文件操作 package ch15; import java.io.*; /** * Created by Jiqing on 2016/12/28. */ public class FileTest ...

  7. openfire源码修改聊天消息发送内容

    /** * $RCSfile: MessageRouter.java,v $ * $Revision: 3007 $ * $Date: 2005-10-31 13:29:25 -0300 (Mon, ...

  8. Deep Learning 3_深度学习UFLDL教程:预处理之主成分分析与白化_总结(斯坦福大学深度学习教程)

    1PCA ①PCA的作用:一是降维:二是可用于数据可视化: 注意:降维的原因是因为原始数据太大,希望提高训练速度但又不希望产生很大的误差. ② PCA的使用场合:一是希望提高训练速度:二是内存太小:三 ...

  9. 项目打包文件build.xml

    Make命令其实就是一个项目管理工具,而Ant所实现功能与此类似.像make,gnumake和nmake这些编译工具都有一定的缺陷,但是Ant却克服了这些工具的缺陷.最初Ant开发者在开发跨平台的应用 ...

  10. Oracle 优化 - CPU 问题

    作为 OLTP 应用,一般不太有 CPU 问题,比较少 - 毕竟大多数问题都是 IO 引起:但是偶尔也会有. 问题判断 很简单 - OS 出现 CPU 很高的问题,持续高于 90% 应用可能会表现慢 ...