Android广播接收器BroadcastRceiver
一、使用BroadcastRceiver
1、创建BroadcastRceiver(MyRceiver),重写OnReceiver:
public void onReceive(Context context, Intent intent) {
System.out.println("接收到了消息,消息内容是:"+intent.getStringExtra("data"));
}
2、在主布局添加一个按钮:
<Button
android:text="发送消息"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSendMsg" />
3、实现发送消息按钮的监听功能:
findViewById(R.id.btnSendMsg).setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.btnSendMsg:
Intent i = new Intent(this,MyReceiver.class);
i.putExtra("data","Android");
sendBroadcast(i);
break;
}
}
二、动态注册和注销BroadcastReceiver
在某种情况,我们并不希望BroadcastReceiver始终从处于监听状态。这就需要我们动态地注册和注销BroadcastReceiver。
1、删除AndroidManifest.xml中关于MyReceiver的配置,则不能再发送消息:
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"></receiver>
2、在主布局中添加注册和注销按钮:
<Button
android:text="注册接收器"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnRes" />
<Button
android:text="注销接收器"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnUnres" />
3、MyReceiver文件:
public static final String ACTION = "com.w.learnbroadcastrceiver.intent.action.MyReceiver";
4、功能实现:
//防止重复注册多个Receive
private MyReceiver receiver = null;
findViewById(R.id.btnRes).setOnClickListener(this);
findViewById(R.id.btnUnres).setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()){
case R.id.btnSendMsg:
// Intent i = new Intent(this,MyReceiver.class);
Intent i = new Intent(MyReceiver.ACTION); //隐式Intent
i.putExtra("data","Android");
sendBroadcast(i);
break;
case R.id.btnRes:
if(receiver == null){
receiver = new MyReceiver();
registerReceiver(receiver,new IntentFilter(MyReceiver.ACTION));
}
break;
case R.id.btnUnres:
if(receiver != null){
unregisterReceiver(receiver);
receiver = null;
}
break;
}
}
三、BroadcastRceiver的优先级
1、Androidmanifest.xml中后注册的Receiver先接收到消息,即后注册的Receiver优先级高。
<intent-filter>中属性 android:priority="10",优先级越高就越先接收到消息
2、优先级高的receiver不想让后续的receiver接收到消息,如何做(MyReceiver.java)?
public void onReceive(Context context, Intent intent) {
System.out.println("MyReceiver 接收到了消息");
abortBroadcast(); //中断广播(MyReceiver比MyReceiver1优先级高的情况)
}
而MainActivity.java中:
//sendBroadcast(i);
sendOrderedBroadcast(i,null); //参数分别代表Intent对象和权限
Android广播接收器BroadcastRceiver的更多相关文章
- android广播接收器
Android程序创建广播接收器继承BroadcastReceiver Android广播接收器需要在AndroidManifest.xml文件中声明: <recevie android:nam ...
- Xamarin.Android广播接收器与绑定服务
一.前言 学习了前面的活动与服务后,你会发现服务对于活动而言似乎就是透明的,相反活动对于服务也是透明的,所以我们还需要一中机制能够将服务和活动之间架起一座桥梁,通过本节的学习,你将会学到广播与绑定服务 ...
- Android广播接收器Broadcast Receiver-android学习之旅(十二)
首先继承BroadcastReceiver类,并在manifest中注册 public class MyReceiver extends BroadcastReceiver { public MyRe ...
- Android广播接收器和Activity间传递数据
Activity向广播接收器传递数据很简单,只需要在发送广播前将数据put进Intent中就行了. 广播接收器怎么向Activity传送数据?这里要用到接口,通过在广播接收器里定义一个接口,然后让接收 ...
- Android广播接收器里弹出对话框
不多说,直接上车... public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(fina ...
- android广播接收器BroadcastReceiver
首先看一下什么是 BroadcastReceiver BroadcastReceiver:直译是"广播接收者",所以它的作用是用来接收发送过来的广播的. 那我们有必要知道:什么是广 ...
- (八)Android广播接收器BroadcastReceiver
一.使用Broadcast Reciver 1.右击java文件夹,new->other->Broadcast Receiver后会在AndroidManifest.xml文件中生成一个r ...
- Android -- 简单广播接收与发送(2)--动态注册广播接收器
1. 效果图
- Android基础总结(4)——广播接收器
在Android中的每个应用程序可以对自己感兴趣的广播进行注册,这样该程序就只会接收自己所关心的广播内容,这些广播可能来自于系统的,也可能来自于其他应用程序的.Android提供了一整套完整的API, ...
随机推荐
- 浅谈Linux内存管理机制
经常遇到一些刚接触Linux的新手会问内存占用怎么那么多?在Linux中经常发现空闲内存很少,似乎所有的内存都被系统占用了,表面感觉是内存不够用了,其实不然.这是Linux内存管理的一个优秀特性,在这 ...
- LVM 管理减少swap分区空间增加到根分区
简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现 ...
- [Top-Down Approach] Chatper 3 Notes
这里留下空白,提醒自己,第一章第二章尚待整理回顾. 此处缺了3.6/3.7两节拥塞控制的内容
- transient关键字的作用
代码如下: import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutpu ...
- Selenium-java-Log4j环境搭建和
1 导入Log4j ,我这版本是1.2.17 自己选择版本 **别告诉我不会导入 2 Path 奶瓶 3 创建一个与src同目录文件 命名为 log4.properties 4 文件的内容是, ...
- Lis日常维护
1.[问题]护士站打印LIs条码,出来是PDF格式的 [解决]在文件夹Client\NeusoftLis\Xml\Print.xml中把BarcodePrint Name的值改成安装的斑马打印机名(不 ...
- UOJ #58 【WC2013】 糖果公园
题目链接:糖果公园 听说这是一道树上莫队的入门题,于是我就去写了--顺便复习了一下莫队的各种姿势. 首先,我们要在树上使用莫队,那么就需要像序列一样给树分块.这个分块的过程就是王室联邦这道题(vfle ...
- 1366分辨率其实是1368分辨率,firefox a标签点击有虚线
1,通过intel 集成显卡的软件自定义一个1366分辨率,发现是1368的分辨率,@media screen and (max-deivce-width:1368px)才有效果,并且在同事电脑默认分 ...
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...