Receiving intents

原文地址:
https://github.com/excilys/androidannotations/wiki/Receiving-intents

接收意图

@Receiver注释通知你的代码的意图,而不必手工申报和注册一个BroadcastReceiver。例如:

@EActivity
public 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, fragmentsservices (以及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.Parcelablejava.io.Serializable 序列化的被@Receiver.Extra注释的参数,key就是用@Receiver.Extra 声明的值。

Data Schemes

dataScheme参数可以设置一个或多个dataSchemes,这些值会交给由Receiver处理。

@EActivity
public 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发生和显示时可以使用的值。

TablesRegisterUnregisterActivityFragmentService
OnCreateOnDestroyonCreateonDestroyXXX
OnStartOnStoponStartonStopXX
OnResumeOnPauseOnAttachOnDetachonAttachX

这里有一段示例代码:

@EFragment
public 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。

@EService
public class MyService extends Service {

  @Receiver(actions = "org.androidannotations.ACTION_1", local = true)
  protected void onAction1OnCreate() {  
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

}

转载于:https://my.oschina.net/u/1023510/blog/535769

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值