一、Broadcast简介

Broadcast是Android的四大组件之一。可分为:

1、普通广播  发送一个广播,所有监听该广播的广播接收者都可以监听到改广播。

2、异步广播  当处理完之后的Intent,依然存在,这时候registerReceiver(BroadcastReceiver,IntentFilter) 还能收到他的值,直到你把它去掉,不能将处理结果传给下一个接收者,无法终止广播。

3、有序广播  按照接收者的优先级顺序接收广播,优先级别在intent-filter中的priority中声明,-1000到1000之间,值越大,优先级越高.可以终止广播意图的继续传播.接收者可以篡改内容.

二、广播Broadcast实现过程

1、注册

a、配置文件注册

 <!-- 注册自定义静态广播接收器 -->
<receiver android:name="com.example.androidtest.broadcast.MyReceiver">
  <intent-filter >
  <action android:name="MyAction"/>
</intent-filter>
</receiver>

b、代码注册

 MyReceiver myReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("MyAction");
registerReceiver(myReceiver,filter);

2、发送广播

 Intent intent = new Intent();
intent.setAction("MyAction");
intent.putExtra("msg", "这是广播发送的消息");
sendBroadcast(intent);

3、接收类的实现

继承BroadcastReceiver并重写onReceive方法。

 public class MyReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "广播消息是:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
} }

三、若内部类作为广播接收类如何实现

1、注册

a、配置文件注册

1 <!-- 注册自定义静态广播接收器 -->
2 <receiver android:name="com.example.androidtest.broadcast.BroadcastTest$MyReceiver"> <!-- 注意内部类写法:普通类$内部类 -->
3   <intent-filter >
4   <action android:name="MyAction"/>
5 </intent-filter>
6</receiver>

b、代码注册

1 MyReceiver myReceiver = new MyReceiver();
2 IntentFilter filter = new IntentFilter();
3 filter.addAction("MyAction");
4 registerReceiver(myReceiver,filter);

2、发送广播

1 Intent intent = new Intent();
2 intent.setAction("MyAction");
3 intent.putExtra("msg", "这是广播发送的消息");
4 sendBroadcast(intent);

3、接收类的实现

注意:若在配置文件中注册则该类必须声明为static,否则找不到,若在代码中注册可省略static修饰

1 public static class MyReceiver extends BroadcastReceiver
2 {
3 public void onReceive(Context context, Intent intent)
4 {
5 Toast.makeText(context, "广播消息是:" + intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
6 }
7
8 }

四、用途

开机、锁屏、电量低……

五、注意

生命周期只有十秒左右,如果在 onReceive() 内做超过十秒内的事情,就会报错 。

每次广播到来时 , 会重新创建 BroadcastReceiver 对象 , 并且调用 onReceive() 方法 , 执行完以后 , 该对象即被销毁 . 当 onReceive() 方法在 10 秒内没有执行完毕, Android 会认为该程序无响应 . 所以在BroadcastReceiver 里不能做一些比较耗时的操作 , 否侧会弹出 ANR(Application NoResponse) 的对话框

部分内容取自:http://yangguangfu.iteye.com/blog/1063732

Android常用组件Broadcast介绍的更多相关文章

  1. Android常用组件【转】

    UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...

  2. Android常用组件

    UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...

  3. Android新组件RecyclerView介绍,其效率更好

    今天我们首先来说为什么要介绍这个新组件RecyclerView,因为前几天我发布了一个常用面试题ListView的复用及如何优化的文章,介绍给一些开发者,但是我看到有关的反馈说:现在都不再用listv ...

  4. android 四大组件Broadcast Receiver

    本文介绍Broadcast Receiver,包括几部分内容:Broadcast Receiver概述及实例.自定义Broadcast Receiver.Broadcast Receiver的实现细节 ...

  5. Android四大组件--Broadcast Receiver具体解释

    本文主要讲述了: 一.BroadcastReceiver概述: 二.BroadcastReceiver事件分类 三.BroadcastReceiver事件的编程流程 四.两类BroadcastRece ...

  6. android常用http框架介绍

    测试数据 1.HttpURLConnection:在Android 2.2版本之前,HttpClient拥有较少的bug,因此使用它是最好的选择.而在Android 2.3版本及以后,HttpURLC ...

  7. Android四大组件-Broadcast Receiver

    http://www.jianshu.com/p/1013a366cc72 http://www.jianshu.com/p/ca3d87a4cdf3 前言 基础知识. 1.Android广播分为两个 ...

  8. Android 架构组件 Room 介绍及使用

    关于Room Room是Google官方提供的数据库ORM框架,使用起来非常方便.Room在SQLite上提供了一个抽象层,以便在利用SQLite的全部功能的同时能更加流畅的访问数据库. Room中三 ...

  9. Android常用组件之AutoCompleteTextView

    安卓组件中,凡是需要配置数据的组件,一般都是用Adapter配置. AutoCompleteTextView的使用方法与ListView类似,也是用setAdapter来设置数据. MultiAuto ...

随机推荐

  1. C#基础学习心得(一)

    类的成员 数据成员:字段,常量(const) 函数成员:方法,属性,索引器,构造函数,析构函数,事件 类的声明 实例成员:对象相关性,不同于同一类的其他实例 静态成员:常量,static修饰的字段,方 ...

  2. html中把li前面的的小圆点换成小图片的方法

    li { list-style: none; background: url(../img/li_dis.png) no-repeat left; padding-left: 20px; }

  3. 未能加载文件或程序集“System.Web.Helpers, Version=2.0.0.0(转)

    在本地终于用上了ASP.NET MVC4自带的认证功能,但放到生产服务器上就出问题了:打开注册页面没问题,但一点下注册按钮就报错了: 未能加载文件或程序集“System.Web.Helpers, Ve ...

  4. c# 调用 友盟api

    今天要使用友盟的推送API来给我的app进行推送信息,调试了好久,老是返回500错误,最终在友盟的技术人员支持下完成了此操作,在此多谢友盟技术和客服人员. 把发方法和注意事项贴出来供大家参考. pub ...

  5. 1.Asp.net处理请求的流程

    .NET平台处理HTTP请求的过程大致如下: 1. IIS得到一个请求: 2.查询脚本映射扩展,然后把请求映射到aspnet_isapi.dll文件 3.代码进入工作者进程(IIS5里是aspnet_ ...

  6. execlp函数使用

    原文:http://blog.sina.com.cn/s/blog_6a1837e901011167.html execlp(从PATH 环境变量中查找文件并执行)   相关函数:   fork,ex ...

  7. jsp页面获取服务器时间

    Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MON ...

  8. 0122——UITabBarController

    UITabBarController是IOS中很常用的一个viewController.UITabBarController通常作为整个程序的rootViewController,而且不能添加到别的c ...

  9. UIView的常用方法

    bringSubviewToFront: 把指定的子视图移动到顶层 - (void)bringSubviewToFront:(UIView *)view 参数 view 需要移到顶层的视图 conve ...

  10. (原+转)pycharm中传入命令行参数

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5670821.html 参考网址: http://zhidao.baidu.com/question/5 ...