Enhance broadcastreceivers - WonderCsabo/androidannotations GitHub Wiki

Since AndroidAnnotations 2.4

You can enhance an Android BroadcastReceiver with the @EReceiver annotation:

@EReceiver
public class MyReceiver extends BroadcastReceiver {

}

You can then start using most AA annotations, except the ones related to views and extras:

@EReceiver
public class MyReceiver extends BroadcastReceiver {

  @SystemService
  NotificationManager notificationManager;

  @Bean
  SomeObject someObject;
  
}

@ReceiverAction

Since AndroidAnnotations 3.2

The @ReceiverAction annotation allows to simply handle Broadcasts in an enhanced Receiver.

By default the Method name is used to determine the Action to be handled, but you can use the value of @ReceiverAction to pass another Action name.

Method annotated with @ReceiverAction may have the following parameters :

  • An android.content.Context which will be the context given in void onReceive(Context context, Intent intent)
  • An android.content.Intent which will be the intent given in void onReceive(Context context, Intent intent)
  • Any native, android.os.Parcelable or java.io.Serializable parameters annotated with @ReceiverAction.Extra which will be the extra put in the intent. The key of this extra is the value of the annotation @ReceiverAction.Extra if set or the name of the parameter.
@EReceiver
public class MyIntentService extends BroadcastReceiver {

	@ReceiverAction("BROADCAST_ACTION_NAME")
	void mySimpleAction(Intent intent) {
		// ...
	}

	@ReceiverAction
	void myAction(@ReceiverAction.Extra String valueString, Context context) {
		// ...
	}

	@ReceiverAction
	void anotherAction(@ReceiverAction.Extra("specialExtraName") String valueString, @ReceiverAction.Extra long valueLong) {
		// ...
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		// empty, will be overridden in generated subclass
	}
}

Since AndroidAnnotations 3.3

Note: Since BroadcastReceiver#onReceive is abstract, you have to add an empty implementation. For convenience, we provide the AbstractBroadcastReceiver class, which implements that method, so you do not have to do in your actual class if you derive it.

Note: You can now pass multiple actions that should be handled by using the value of @ReceiverAction.

    @ReceiverAction({"MULTI_BROADCAST_ACTION1", "MULTI_BROADCAST_ACTION2"})
    void multiAction(Intent intent) {
        // ...
    }

Data Schemes

With the dataScheme parameter you can set one or more dataSchemes that should be handled by the Receiver.

@EReceiver
public class MyIntentService extends BroadcastReceiver {

  @ReceiverAction(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.
  }
  
  @ReceiverAction(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 @Receiver annotation

Your activity/fragment/service can be notified of intents using the @Receiver annotation, instead of declaring a BroadcastReceiver.

@EActivity
public class MyActivity extends Activity {

  @Receiver(actions = "org.androidannotations.ACTION_1")
  protected void onAction1() {

  }

}

More information about the @Receiver annotation.

Since AndroidAnnotations 3.1