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, ...
随机推荐
- MySQL同步常见问题解答(自己的小心得)
前几天刚刚注册了博客园,我想写一些技巧性的教程,今天给大家分享一个MySQL同步常见问题解答. Q:如果主服务器正在运行并且不想停止主服务器,怎样配置一个从服务器? A:有多种方法.如果你在某时间点做 ...
- 15天玩转redis —— 第十篇 对快照模式的深入分析
我们知道redis是带有持久化这个能力了,那到底持久化成到哪里,持久化成啥样呢???这篇我们一起来寻求答案. 一:快照模式 或许在用Redis之初的时候,就听说过redis有两种持久化模式,第一种是S ...
- Android使用最小宽度限定符时最小宽度的计算
Android开发中最头疼的问题之一就是兼容不同尺寸和分辨率的设备.这里推荐一篇总结的比较完整的<Android开发:最全面.最易懂的Android屏幕适配解决方案>.这篇文章对屏幕兼容的 ...
- python-open文件处理
python内置函数open()用于打开文件和创建文件对象 语法 open(name[,mode[,bufsize]]) name:文件名 mode:指定文件的打开模式 r:只读 w:写入 a:附加 ...
- OpenWRT镜像爬虫搭建本地源
网上的爬虫不能用,还是先表达谢意,不过我比较懒不喜欢重复写别人写的教程,只贴出修改,怎么用自己看教程吧. 我自己改了一版可以正常爬: #!/usr/bin/env python #coding=utf ...
- MFC快速入门 - 菜单
本文仅用于学习交流,商业用途请支持正版!转载请注明:http://www.cnblogs.com/mxbs/p/6231104.html 打开VS2010,依次打开File – New – Proje ...
- JDBC中的Statement和PreparedStatement的区别
JDBC中的Statement和PreparedStatement的区别
- [LeetCode] Reconstruct Itinerary 重建行程单
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- [LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- [LeetCode] Read N Characters Given Read4 用Read4来读取N个字符
The API: int read4(char *buf) reads 4 characters at a time from a file.The return value is the actua ...