首先看一下什么是 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. js基础的知识整理

    一.操作样式: .style   操作行间样式 .className 修改class 二.操作属性 1. .  更简单,操作已有的属性 2. [] 更灵活,点能做的,方括号都能做.方括号中放的是字符串 ...

  2. Spring整合Hibernate。。。。

    环境搭建,在eclipse中导入spring和hibernate框架的插件,和导入所有使用到的架包 首先,hibernate的创建: 建立两个封装类,其中封装了数据库中表的属性,这儿只写属性,gett ...

  3. (原创) cocos2dx使用Curl连接网络(客户端)

    0. 环境: winxpsp3, vs2010, cocos2dx@2.1.4 1. 新建一个Helloworld工程 2. HelloworldScene.h里面重写virtual bool ccT ...

  4. noi 2989 糖果

    题目链接:http://noi.openjudge.cn/ch0206/2989/ 首先,数据很大,直接用背包会re. 这里增加的是对%k 的余数维度.f[i][j] 表示前 i 种糖果取到总颗数模 ...

  5. 自己写的java用jxl导出到excel工具

    package com; import java.io.BufferedOutputStream; import java.io.File; import java.io.IOException; i ...

  6. c++实现螺旋矩阵分析总结

    螺旋矩阵,是这么一个东西: 1   2   3 8   9   4 7   6   5 这是一个,n*n的矩阵,由外向里一次递增,一环一环,就好像一个螺旋一样.不难想象,如果n=5,那么应该是这样的: ...

  7. [问题2014A10] 复旦高等代数 I(14级)每周一题(第十二教学周)

    [问题2014A10]  设 \(A\) 为 \(n\) 阶实方阵满足 \(AA'=I_n\) (即 \(A\) 为 \(n\) 阶正交阵), 证明: \[\mathrm{rank}(I_n-A)=\ ...

  8. jquery.validate使用 - 2

    jQuery.validate.js API说明 参考http://ideabean.javaeye.comPlugin methods Name Type validate( options ) R ...

  9. C# 调用 Outlook发送邮件实例

    添加引用:Microsoft.Office.Interop.Outlook using System; using System.Collections.Generic; using System.L ...

  10. Caffe + Ubuntu 14.04 64bit + CUDA6.5 + 无GPU 配置

    官网: http://caffe.berkeleyvision.org/installation.html#compilation 参考网站: http://www.cnblogs.com/dupul ...