Receiving intents

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

接收意图

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

1
2
3
4
5
6
7
8
9
@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处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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发生和显示时可以使用的值。
| Tables| Register| Unregister|Activity|Fragment|Service|
| ——– |:—-:|:—–:|:—-:|:—–:|:—-:|
| OnCreateOnDestroy | onCreate | onDestroy | X| X | X |
| OnStartOnStop | onStart| onStop|X|X| |
| OnResumeOnPause | OnAttachOnDetach | onAttach ||X||

这里有一段示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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。

1
2
3
4
5
6
7
8
9
10
11
12
13
@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;
}

}