Eventbus
2016-02-24 16:23
260 查看
(1)自定义一个类,可以是空类,比如:
(2)在要接收消息的页面注册:
(3)发送消息
(4)接受消息的页面实现(共有四个函数,各功能不同,这是其中之一,可以选择性的实现,这里先实现一个):
(5)解除注册
在要接收消息的页面注册EventBus:
在上面的GIF图片的演示中,大家也可以看到,我们是要在MainActivity中接收发过来的消息的,所以我们在MainActivity中注册消息。
通过我们会在OnCreate()函数中注册EventBus,在OnDestroy()函数中反注册。所以整体的注册与反注册的代码如下:
4、发送消息
发送消息是使用EventBus中的Post方法来实现发送的,发送过去的是我们新建的类的实例!
[java] view plain copy 在CODE上查看代码片派生到我的代码片
EventBus.getDefault().post(new FirstEvent(“FirstEvent btn clicked”));
完整的SecondActivity.java的代码如下:
接受消息
public class AnyEventType { public AnyEventType(){} }
(2)在要接收消息的页面注册:
eventBus.register(this);
(3)发送消息
eventBus.post(new AnyEventType event);
(4)接受消息的页面实现(共有四个函数,各功能不同,这是其中之一,可以选择性的实现,这里先实现一个):
public void onEvent(AnyEventType event) {}
(5)解除注册
eventBus.unregister(this);
在要接收消息的页面注册EventBus:
在上面的GIF图片的演示中,大家也可以看到,我们是要在MainActivity中接收发过来的消息的,所以我们在MainActivity中注册消息。
通过我们会在OnCreate()函数中注册EventBus,在OnDestroy()函数中反注册。所以整体的注册与反注册的代码如下:
package com.example.tryeventbus_simple; import com.harvic.other.FirstEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button btn; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册EventBus EventBus.getDefault().register(this); btn = (Button) findViewById(R.id.btn_try); tv = (TextView)findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } @Override protected void onDestroy(){ super.onDestroy(); EventBus.getDefault().unregister(this);//反注册EventBus } }
4、发送消息
发送消息是使用EventBus中的Post方法来实现发送的,发送过去的是我们新建的类的实例!
[java] view plain copy 在CODE上查看代码片派生到我的代码片
EventBus.getDefault().post(new FirstEvent(“FirstEvent btn clicked”));
完整的SecondActivity.java的代码如下:
package com.example.tryeventbus_simple; import com.harvic.other.FirstEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class SecondActivity extends Activity { private Button btn_FirstEvent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_FirstEvent = (Button) findViewById(R.id.btn_first_event); btn_FirstEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EventBus.getDefault().post( new FirstEvent("FirstEvent btn clicked")); } }); } }
接受消息
package com.example.tryeventbus_simple; import com.harvic.other.FirstEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button btn; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); btn = (Button) findViewById(R.id.btn_try); tv = (TextView)findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } public void onEventMainThread(FirstEvent event) { String msg = "onEventMainThread收到了消息:" + event.getMsg(); Log.d("harvic", msg); tv.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } @Override protected void onDestroy(){ super.onDestroy(); EventBus.getDefault().unregister(this); } }
相关文章推荐
- iOS持久化存储-CoreData简介
- 用itextpdf生成水印来代替pdf的替换问题
- 用idea工具创建maven的web工程
- Mac上Svn使用:Cornerstone可视化工具
- Xtrabackup安装与配置
- HDU 1829 A bug's life
- Xcode崩溃日志分析工具symbolicatecrash用法
- Linux中创建别名
- activiti 源码笔记之startProcess
- session过期问题
- MySQL设置当前时间为默认值的方法
- 让CloudStack使用KVM创建Windows实例成功识别并挂载数据盘
- 【请分享!】丨FacebookCEO告诉你人生中必读的6本书
- AFNetWorking 3.0上传图片
- Java调用不支持JNI的Linux so文件
- 排序算法
- leetcode 292. Nim Game(拿石子游戏)
- 浮点转字符串性能比较
- 宁要混蛋不要老好人——一个互联网创业者的用人观
- PHP-xml-1