Android EventBus的简单使用
2015-11-16 21:21
489 查看
Activity与Service之间的通信(组件之间的通信)
1、Activity 绑定Service形式
2、使用BroadcastReceiver通信
3、定义interface进行回调
4、使用EventBus通信:
EventBus是一个事件订阅/发布模型,用以简化Android内部组件之间的数据通信。
Android Studio中使用EventBus:
gradle:
主要涉及函数:
1)注册eventbus:
2)注销eventbus:
4)定义事件响应onEvent:
响应事件共有如下四种:
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
5)发送消息事件:
具体应用:
1)两个用以保存事件具体内容的自定义类:
2)Activity:
3)Service:
1、Activity 绑定Service形式
2、使用BroadcastReceiver通信
3、定义interface进行回调
4、使用EventBus通信:
EventBus是一个事件订阅/发布模型,用以简化Android内部组件之间的数据通信。
Android Studio中使用EventBus:
gradle:
compile 'de.greenrobot:eventbus:2.4.0'
主要涉及函数:
1)注册eventbus:
EventBus.getDefault().register(this);
2)注销eventbus:
EventBus.getDefault().unregister(this);3)定义事件Event
public class ServiceToActivityEvent
4)定义事件响应onEvent:
// 响应Service发送来的Event public void onEventMainThread(ServiceToActivityEvent event) { String msg = event.getMsg(); mytxt.setText(msg); }onEventMainThread是四种订阅函数的一种,
响应事件共有如下四种:
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
5)发送消息事件:
EventBus.getDefault().post(new ServiceToActivityEvent("MyEvent Send!"));
具体应用:
1)两个用以保存事件具体内容的自定义类:
public class ActivityToServiceEvent { private String msg_text; public ActivityToServiceEvent(String msg_text) { this.msg_text = msg_text; } public String getMsg() { return msg_text; } }
public class ServiceToActivityEvent { private String msg_text; public ServiceToActivityEvent(String msg_text) { this.msg_text = msg_text; } public String getMsg() { return msg_text; } }
2)Activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private MyView myView;
private TextView mytxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 注册EventBus
EventBus.getDefault().register(this);
initView();
}
private void initView() {
mytxt = (TextView) findViewById(R.id.mytxt);
mytxt.setOnClickListener(this);
Intent intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
// 响应Service发送来的Event public void onEventMainThread(ServiceToActivityEvent event) { String msg = event.getMsg(); mytxt.setText(msg); }
@Override
protected void onDestroy(){
super.onDestroy();
// 注销EventBus
EventBus.getDefault().unregister(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.mytxt:
EventBus.getDefault().post(new ActivityToServiceEvent("mytxt_OnClick"));
break;
}
}
}
3)Service:
public class MyService extends Service { @Override public void onCreate() { super.onCreate(); EventBus.getDefault().register(this); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); EventBus.getDefault().post(new ServiceToActivityEvent("MyEvent Send!")); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); } @Override public void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } public void onEventMainThread(ActivityToServiceEvent event) { String msg = event.getMsg(); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } }
相关文章推荐
- android屏幕录像
- Xamarin.Android广播接收器
- Android 一些基本组件的使用
- Android Studio 配置 Gradle 直接运行打包(relase)程序 图文详解
- android带返回按钮的自定义标题栏布局文件详解
- Android 实现LazyFragment
- 目前和接下来的android
- Android基础
- Android APK反编译就这么简单 详解(附图)
- Android开发日记(一)
- android菜鸟修炼记
- Android项目——导入工程出错的解决问题
- Android 学习笔记之AndBase框架学习(七) SlidingMenu滑动菜单的实现
- Android打包安装过程
- [Android] 关于Android的.so文件你所需要知道的 - 简书
- android四大组件之Service 电话录音
- 深入浅出 - Android系统移植与平台开发(十一) - Sensor HAL框架分析之一
- 彻底明白Android中AIDL及其使用
- android 之 自定义圆形头像
- android应用层相关设置及命令(二)