forked from androidannotations/androidannotations
-
Notifications
You must be signed in to change notification settings - Fork 0
Receiving intents
PerfectCarl edited this page May 29, 2014
·
17 revisions
Since AndroidAnnotations 3.1
The @Receiver annotation let your code be notified of intents without having to manually declare and register a BroadcastReceiver.
@EActivity
public class ActivityWithValidReceiver 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 supports activities, fragments and services.
The annotation creates and registers programmatically a BroadcastReceiver during the lifecycle of the the parent class (Activity, Fragment or Service) according to the registerAt optional parameter.
@EFragment
public class FragmentWithValidReceiver 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 registration may occur at:
- OnCreateOnDestroy (default)
- OnAttachOnDetach
- OnResumeOnPause
- OnStartOnStop
The optional parameter local register the BroadcastReceiver locally using LocalBroadcastManager
@EService
public class ServiceWithValidReceiver extends Service {
@Receiver(actions = "org.androidannotations.ACTION_1", local = true)
protected void onAction1OnCreate() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}AndroidAnnotations was created by Pierre-Yves Ricau and is sponsored by eBusinessInformations.
09/11/2014 The 3.2 release is out !
- Get started!
- Download
- Cookbook, full of recipes
- Customize annotation processing
- List of all available annotations
- Release Notes
- Examples
- Read the FAQ
- Join the Mailing list
- Create an issue
- Tag on Stack Overflow