Receiving intents
来源:互联网 发布:淘宝网韩版针织开衫 编辑:程序博客网 时间:2024/05/23 15:41
原文地址:
https://github.com/excilys/androidannotations/wiki/Receiving-intents
接收意图
@Receiver
注释通知你的代码的意图,而不必手工申报和注册一个BroadcastReceiver。例如:
@EActivitypublic class MyActivity extends Activity { @Receiver(actions = "org.androidannotations.ACTION_1") protected void onAction1() { // Will be called when an org.androidannotations.ACTION_1 intent is sent. }}
@Receiver
注释支持 activities, fragments 和 services (以及intent services)。
参数说明
你可能只有一个android.content.intent
或没有参数。
使用@Receiver
注解的方法可能有以下参数:
- 一个在方法
void onReceive(Context context, Intent intent)
中给定的上下文参数android.content.Context
。 - 在方法
void onReceive(Context context, Intent intent)
中给定的意图android.content.Intent
。 - 任何放入Intent中被本地
android.os.Parcelable
或java.io.Serializable
序列化的被@Receiver.Extra
注释的参数,key就是用@Receiver.Extra
声明的值。
Data Schemes
dataScheme
参数可以设置一个或多个dataSchemes,这些值会交给由Receiver处理。
@EActivitypublic class MyActivity extends Activity { @Receiver(actions = android.content.Intent.VIEW, dataSchemes = "http") protected void onHttp() { // Will be called when an App wants to open a http website but not for https. } @Receiver(actions = android.content.Intent.VIEW, dataSchemes = {"http", "https"}) protected void onHttps() { // Will be called when an App wants to open a http or https website. }}
Registration(注册)
注释以编程方式在父类(Activity, Fragment or Service)的生命周期内注册和注销BroadcastReceiver。 registerAt
可选参数指定注册/注销时发生。默认值是OnCreateOnDestroy。
下表列出了registerAt
值,当注册/ deregisration发生和显示时可以使用的值。
这里有一段示例代码:
@EFragmentpublic class MyFragment extends Fragment { @Receiver(actions = "org.androidannotations.ACTION_1") protected void onAction1RegisteredOnCreateOnDestroy() { } @Receiver(actions = "org.androidannotations.ACTION_2", registerAt = Receiver.RegisterAt.OnAttachOnDetach) protected void onAction2RegisteredOnAttachOnDetach(Intent intent) { } @Receiver(actions = "org.androidannotations.ACTION_3", registerAt = Receiver.RegisterAt.OnStartOnStop) protected void action3RegisteredOnStartOnStop() { } @Receiver(actions = "org.androidannotations.ACTION_4", registerAt = Receiver.RegisterAt.OnResumeOnPause) protected void action4RegisteredOnResumeOnPause(Intent intent) { }}
Local broadcasting(本地广播)
可选参数的local
注册BroadcastReceiver本地使用LocalBroadcastManager 来代替父类上下文(activity,fragment或service)。
其中 local
的默认值为false。
@EServicepublic class MyService extends Service { @Receiver(actions = "org.androidannotations.ACTION_1", local = true) protected void onAction1OnCreate() { } @Override public IBinder onBind(Intent intent) { return null; }}
0 0
- Receiving intents
- (7)Intents
- Common Intents
- Android Intents
- Common Intents
- Common Intents
- Blind Receiving盲接收
- Receiving and Dispatching Messages
- Sending and Receiving Packets
- Receiving Module Data Flow
- android Error receiving broadcast
- SRN Receiving Failure
- Receiving Transactions SQL
- Silent Receiving of SMS messages
- Receiving network data on Emulator
- Receiving Content from Other Apps
- 5 Sending and Receiving Messages
- Android之Intents
- 属性动画
- HDOJ 不容易系列之(3)—— LELE的RPG难题 2045
- 39. PHP 类型运算符
- 关于凸优化的一些简单概念
- linux基础问题(一)
- Receiving intents
- Hibernate缓存管理
- 第五周项目一:体验常成员函数
- jfreechart折线图示例
- c++模板元编程七:类型判断的traits类
- jQuery Ajax 实例 ($.ajax、$.post、$.get)
- BruteForceStringMatching2
- 使用Camera Calibration Toolbox for Matlab进行摄像机标定
- Mac OS X的入门文档