BroadcastReceiver学习笔记


一直都知道这个类和使用这个类,但是从来没有认真使用过。今天总结一下。


顾名思义广播接收者。 


对于这个类的定义和声明 可以参看该网站的翻译解读broadcastReceiver  比较全面的翻译了SDK 我个人认为,它翻译的很到位。


使用方法 , 作为通讯手段,和接受处理段的功能。它可以动态/静态注册。


动态注册 android.content.Context.registerReceiver(BroadcastReceiver receiver,IntentFilter filter) 通过调用该方法。


前面是 BroadCastReceiver 对象,用于处理消息。 在对象的public void onReceive(Context arg0, Intent intent) 中处理。

通过 匹配 Intent.getAction() 匹配字符串 来进行判断,这没啥说的,说一下后面的IntentFilter 意图过滤器,挺巧妙的策略,要不然只要注册,就能监听说有消息,性能就有问题了。


SDK上写的匹配规则很明显

A match is based on the following rules. Note that for an IntentFilter to match an Intent, three conditions must hold: the action and category must match, and the data (both the data type and data scheme+authority+path if specified) must match


如果要进行匹配需要满足三个条件:action 和 category 必须匹配 数据必须匹配(我没用过太多这个数据) 


干说没意思 贴几行代码




下面说一下静态注册 在SDK同也提了一下 

IntentFilter objects are often created in XML as part of a package's AndroidManifest.xml file, using intent-filter tags.




manifest中注册

public static final int[] AndroidManifestIntentFilter

The intent-filter tag is used to construct an IntentFilter object that will be used to determine which component can handle a particularIntent that has been given to the system. It can be used as a child of the activityreceiver and service tags.

Zero or more actioncategory, and/or data tags should be included inside to describe the contents of the filter.

The optional label and icon attributes here are used with an activity to supply an alternative description of that activity when it is being started through an Intent matching this filter.

Includes the following attributes:

Attribute Description
android:icon A Drawable resource providing a graphical representation of its associated item.
android:label A user-legible name for the given item.
android:logo A Drawable resource providing an extended graphical logo for its associated item.
android:priority Specify the relative importance or ability in handling a particular Intent.

SDK上对于intent-filter的介绍



http://www.cnblogs.com/trinea/archive/2012/11/09/2763197.html 这里有很详细的内容 也可参看


忘了一点 onresume中注册 onpause中释放注册。关于动态注册。


还有一点切记在刚才推荐的SDK翻译中也有提醒,尽量不要再 onReceive 进行异步操作,虽然可以成功,但是会出现不确定的情况,如果五秒没有获取到你希望的数据可能会导致问题的出现。


只是记录我的学习过程!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值