Android RoboGuice 使用指南(19):发送接收Events
来源:互联网 发布:软件测试员待遇 编辑:程序博客网 时间:2024/06/05 20:36
Roboguice 提供了对Context 生命周期相关的事件的send 和receive ,系统缺省支持的事件为:
- OnActivityResultEvent
- OnConfigurationChangedEvent
- OnContentChangedEvent
- OnContentViewAvailableEvent
- OnCreateEvent
- OnDestroyEvent
- OnNewIntentEvent
- OnPauseEvent
- OnRestartEvent
- OnResumeEvent
- OnStartEvent
- OnStopEvent
一个简单的例子如下:
public class MyActivity extends RoboActivity { // You must "register" your listener in the current // context by injecting it. // Injection is commonly done here in the activity, //but can also be done anywhere really. @Inject protected MyListeners myListeners; } // In this example, all the listeners are in a// MyListeners class, but really they could// be anywhere as long as it's registered.// You can even put the listeners directly into// your activity classes if you like!class MyListeners { // Any method with void return type and a // single parameter with @Observes annotation // can be used as an event listener. //This one listens to onResume. public void doSomethingOnResume( @Observes OnResumeEvent onResume ) { Ln.d("Called doSomethingOnResume in onResume"); } // As you might expect, some events can //have parameters. The OnCreate event // has the savedInstanceState parameter that //Android passes to onCreate(Bundle) public void doSomethingElseOnCreate( @Observes OnCreateEvent onCreate ) { Ln.d("onCreate savedInstanceState is %s", onCreate.getSavedInstanceState()) } // And of course, you can have multiple //listeners for a given event. // Note that ordering of listener execution //is indeterminate! public void xxx( @Observes OnCreateEvent onCreate ) { Ln.d("Hello, world!") }}
有关Events的注意事项如下:
- 在Context中使用@Inject定义事件的Listener.
- Event只能在某一特定的Context(Activity)中传送,不能跨Context发送,接受。
- Event除了提供上面列出的Context相关事件外,也可以使用自定义的事件。
- @observes 只能应用到方法上,而不能应用到构造函数上。
下面使用一个自定义事件MyEvent,通过observer 这个自定义事件来发送,接收自定义事件。
public class EventDemo extends RoboActivity { @Inject protected EventManager eventManager; @InjectView (R.id.button) Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.eventdemo); button.setOnClickListener(mGoListener); } private OnClickListener mGoListener = new OnClickListener() { public void onClick(View v) { eventManager.fire(EventDemo.this,new MyEvent()); } }; protected void handleEvent(@Observes MyEvent event){ Toast.makeText(this, "Custom event", Toast.LENGTH_LONG).show(); }} class MyEvent{ //put any memeber you want here.}
本例下载
- Android RoboGuice 使用指南(19):发送接收Events
- Android RoboGuice 使用指南1
- Android RoboGuice 使用指南2
- Android RoboGuice 使用指南3
- Android RoboGuice 使用指南
- Android RoboGuice 使用指南(13):RoboGuice 功能描述
- Android RoboGuice 使用指南(1):概述
- Android RoboGuice 使用指南(11): Scopes
- Android RoboGuice 使用指南(1):概述
- Android RoboGuice 使用指南(3):Bindings 概述
- Android RoboGuice 使用指南(4):Linked Bindings
- Android RoboGuice 使用指南(5):Binding Annotations
- Android RoboGuice 使用指南(6):Instance Bindings
- Android RoboGuice 使用指南(7):@Provides Methods
- Android RoboGuice 使用指南(8):Provider Bindings
- Android RoboGuice 使用指南(9):Untargetted Bindings
- Android RoboGuice 使用指南(14):Inject View
- Android RoboGuice 使用指南(15):Inject Context
- Java文件读写操作大全
- PMD规则设置
- 一步步教你为网站开发Android客户端
- 删除数据库表中重复的记录
- java命令
- Android RoboGuice 使用指南(19):发送接收Events
- Android开发-Intent 和 Activity
- 10个非常有用的PHP函数
- 开发随笔-EntitySQL 学习
- LINUX 下安装网软件的方法wine
- 实际的带宽和资源量来反推制作工艺(竹庄 无边落木)
- apache + tomcat集群
- 50个c/c++源代码网站
- 信息安全中,人是最薄弱的环节