Receiving intents - WonderCsabo/androidannotations GitHub Wiki
Since AndroidAnnotations 3.1
The @Receiver
annotation notifies your code about intents without having to manually declare and register a 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.
}
}
The @Receiver
annotation supports activities, fragments, services and views (and intent services).
Before AndroidAnnotations 3.2
You may have only one android.content.Intent
or no Parameter at all.
Since AndroidAnnotations 3.2
Method annotated with @Receiver
may have the following parameters :
- An
android.content.Context
which will be the context given invoid onReceive(Context context, Intent intent)
- An
android.content.Intent
which will be the intent given invoid onReceive(Context context, Intent intent)
- Any native,
android.os.Parcelable
orjava.io.Serializable
parameters annotated with@Receiver.Extra
which will be the extra put in the intent. The key of this extra is the value of the annotation@Receiver.Extra
if set or the name of the parameter.
@EActivity
public class MyActivity extends Activity {
@Receiver
void myAction(@Receiver.Extra String valueString, Context context) {
// ...
}
@Receiver
void anotherAction(@Receiver.Extra("specialExtraName") String valueString, @Receiver.Extra long valueLong) {
// ...
}
}
Since AndroidAnnotations 3.2
With the dataScheme
parameter you can set one or more dataSchemes that should be handled by the 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.
}
}
The annotation registers and unregisters programmatically a BroadcastReceiver
during the lifecycle of the parent class (Activity, Fragment or Service).
The registerAt
optional parameter specifies when the registration/unregistration occur.
The default value is OnCreateOnDestroy.
The following table lists the values for registerAt
, when the registration/deregisration take place and indicates when the value can be used.
Register | Unregister | Activity | Fragment | Service | View | |
---|---|---|---|---|---|---|
OnCreateOnDestroy | onCreate | onDestroy | x | x | x | |
OnStartOnStop | onStart | onStop | x | x | ||
OnResumeOnPause | onResume | onPause | x | x | ||
OnAttachOnDetach | onAttach* | onDetach* | x | x |
Here an example of code.
@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) {
}
}
The optional parameter local
registers the BroadcastReceiver locally using LocalBroadcastManager
instead of the parent context (Activity, Fragment or Service).
The default value for local
is 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;
}
}