首先看一下什么是 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. PostgreSQL Hot Standby的主备切换

    一. 简介:          PG在9.*版本后热备提供了新的一个功能,那就是Stream Replication的读写分离,是PG高可用性的一个典型应用.其中备库是只读库:若主库出现故障:备库这个 ...

  2. StringBuilder和StringBuffer区别

    一.StringBuilder 一个可变的字符序列.此类提供了一个与StringBuffer兼容的API,但不保证同步.该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程 ...

  3. CMD执行BCP命令

    C:\>BCP "EXEC GetU '2016-7-11' ,'-1'" queryout "C:\\C3Marketing\SummaryReport_test ...

  4. c#链接数据库

    using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; usin ...

  5. Android 编译时注解解析框架

    2.注解 说道注解,竟然还有各种分类,得,这记不住,我们从注解的作用来反推其分类,帮助大家记忆,然后举例强化大家的记忆,话说注解的作用: 1.标记一些信息,这么说可能太抽象,那么我说,你见过@Over ...

  6. RCP:如何移除Toolbar中的Quick Access

    问题 自4.x开始,Quick Access搜索框成为Toolbar的"标准装备",一般删除Actionset的方式似乎不起作用,通过Quick Access,用户很容易访问到RC ...

  7. 【转】ini载入保存类,操作INI配置文件方便的很

    /****************************************************************** * * ^_^ 恶猫 独门商标 挖哈哈 * * QQ:\> ...

  8. sql2008 表名为全数字时查询报错

    今天遇到个很奇葩的问题,在写一个应用程序时需要查询表的数据,但是表名是全数字的,直接查询会报错,于是想到给111的表名加一对中括号:即——>select * from [111] 刚开始还是报错 ...

  9. Displaying Window In Center In Oracle Forms 6i

    Center window automatically  in Oracle Forms 6i, use the following procedure by passing window name ...

  10. Nginx模块之————RTMP模块在Ubuntu上以串流直播HLS视频

    Nginx的安装在Ubuntu上以串流直播HLS视频 https://www.vultr.com/docs/setup-nginx-on-ubuntu-to-stream-live-hls-video