Dependency injection 如何使用Android注释将活动实例注入我的BroadcastReceiver?
我过去常常和简单的广播听众一起玩活动,在那里可以做一些类似textView.setText(“text”)的事情;但听者变得相当大,无法呆在里面,我把它单独放在一节课上。 我喜欢AA,我只想继续从我分离的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
@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可能会有所帮助,但不知道如何替换)