Dependency injection 如何使用Android注释将活动实例注入我的BroadcastReceiver?

Dependency injection 如何使用Android注释将活动实例注入我的BroadcastReceiver?,dependency-injection,android-annotations,Dependency Injection,Android Annotations,我过去常常和简单的广播听众一起玩活动,在那里可以做一些类似textView.setText(“text”)的事情;但听者变得相当大,无法呆在里面,我把它单独放在一节课上。 我喜欢AA,我只想继续从我分离的BroadcastReceiver管理当前活动的视图。但是当我做一些类似的事情时: @EReceiver public class WarningActivityStateListener extends BroadcastReceiver { @RootContext //or Bea

我过去常常和简单的广播听众一起玩活动,在那里可以做一些类似textView.setText(“text”)的事情;但听者变得相当大,无法呆在里面,我把它单独放在一节课上。 我喜欢AA,我只想继续从我分离的BroadcastReceiver管理当前活动的视图。但是当我做一些类似的事情时:

@EReceiver
public class WarningActivityStateListener extends BroadcastReceiver {
    @RootContext //or Bean
    WarningActivity activity;
我有一个错误:

Error:(25, 2) error: org.androidannotations.annotations.RootContext can only be used in a class annotated with @org.androidannotations.annotations.EBean.
如果我尝试在上面添加@EBean,那么我会遇到更糟糕的情况:

Error:(27, 28) error: Something went wrong: Unexpected error in AndroidAnnotations 4.2.0!
You should check if there is already an issue about it on https://github.com/androidannotations/androidannotations/search?q=java.lang.ClassCastException&type=Issues
If none exists, please open a new one with the following content and tell us if you can reproduce it or not. Don't forget to give us as much information as you can (like parts of your code in failure).

您不能这样做,因为
广播接收器
上下文
是接收器本身,它与任何
活动
无关。您只能在此处使用
@App
注释注入应用程序
上下文

编辑

您可以使用
@Receiver
活动中的方法进行注释,该方法将处理接收
意图
广播接收器
注册/注销:

@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.
  }

}

或者您可以创建一个普通的
广播接收器
,并通过事件总线将其
onReceive
方法中的事件发送到
活动
,或者
本地广播接收器

您不能这样做,因为
广播接收器
上下文
就是接收器本身,它与任何
活动
无关。您只能在此处使用
@App
注释注入应用程序
上下文

编辑

您可以使用
@Receiver
活动中的方法进行注释,该方法将处理接收
意图
广播接收器
注册/注销:

@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.
  }

}

或者,您可以创建一个普通的
BroadcastReceiver
,并通过事件总线或
LocalBroadcastReceiver

将事件从其
onReceive
方法发送到
活动
,谢谢。但在这种情况下如何与UI交互?在使用AA之前,我使用WarningActivity创建了一个构造函数。在活动中,它看起来像是私有广播接收器stateReceiver=new WarningActivityStateListener(此)
,在receiver中它是作为
公共警告ActivityStateListener(警告活动){this.activity=activity;}
-因此我可以使用该活动来更改视图。请告知如何将其替换为AA。我认为应用程序的IntentBuilder可能会有所帮助,但不知道如何)谢谢。但在这种情况下如何与UI交互?在使用AA之前,我使用WarningActivity创建了一个构造函数。在活动中,它看起来像是私有广播接收器stateReceiver=new WarningActivityStateListener(此)
,在receiver中它是作为
公共警告ActivityStateListener(警告活动){this.activity=activity;}
-因此我可以使用该活动来更改视图。请告知如何用AA替换它。我认为应用程序的IntentBuilder可能会有所帮助,但不知道如何替换)