Android项目实战(十三):浅谈EventBus
概述:
EventBus是一款针对Android优化的发布/订阅事件总线。
主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service。
线程之间传递消息.优点是开销小,代码更优雅,以及将发送者和接收者解耦。
---------------------------------------------------------------------------------------
下载:
类库源码:https://github.com/greenrobot/EventBus
jar包:http://download.csdn.net/detail/yy1300326388/8727699
---------------------------------------------------------------------------------------
使用:
build.gradle ,如果这种方式 不需要下载类库或者jar包 一句话即可导入
compile 'de.greenrobot:eventbus:2.4.0'
一、EventBus的使用,简单的来说就是5步:创建一个类(具体使用下面介绍),注册,发送消息,接收消息,解除注册
看一个Demo:
实现功能:有两个Activity,第一个Activity 跳转第二个Activity,第二个Activity 点击按钮发送消息,第一个Activity中的TextView显示接收到的这个消息信息
1、写下两个Activity的布局
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 4 android:paddingRight="@dimen/activity_horizontal_margin" 5 android:paddingTop="@dimen/activity_vertical_margin" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:orientation="vertical" 8 tools:context=".MainActivity"> 9 10 <TextView 11 android:layout_gravity="center" 12 android:id="@+id/show_msg" 13 android:text="@string/hello_world" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" /> 16 17 <Button 18 android:id="@+id/to_second_activity" 19 android:layout_gravity="center" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="跳转第二个Activity"/> 23 24 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <Button 7 android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:text="发送一个消息" 10 android:id="@+id/send_msg" /> 11 12 <Button 13 android:id="@+id/btn_finish" 14 android:text="销毁这个Activity,返回第一个Activity" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" /> 17 </LinearLayout>
2、创建一个类,构造方法参数不固定,随便写,空类也可以,用于传递消息,看具体需求
package com.xqx.com.eventbusdemo; public class MyMessage { private String string; public MyMessage(String string) { this.string = string; } public String getString() { return string; } }
3、在你接收消息的页面(第一个Activity)注册和解除注册EventBus,并且获取和处理消息
在onCreate()方法中注册
EventBus.getDefault().register(this);
在onDestroy()方法中取消注册
EventBus.getDefault().unregister(this);
实现获取处理消息的方法,这里先使用onEventMainThread()方法,意思为接收到消息并在UI线程操作
public void onEventMainThread(MyMessage event) { String msg = "onEventMainThread收到了消息:" + event.getString(); show_msg.setText(msg); }
完整代码:
package com.xqx.com.eventbusdemo; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import de.greenrobot.event.EventBus; public class MainActivity extends Activity { //按钮,开启第二个Activity private Button to_second_activity; //文本,显示接收到的消息 private TextView show_msg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); to_second_activity = (Button) findViewById(R.id.to_second_activity); show_msg = (TextView) findViewById(R.id.show_msg); //注册 EventBus.getDefault().register(this); //点击按钮进入到第二个Activity to_second_activity.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(MainActivity.this,SecondActivity.class)); } }); } //接收消息并处理 public void onEventMainThread(MyMessage event) { String msg = "onEventMainThread收到了消息:" + event.getString(); show_msg.setText(msg); } @Override protected void onDestroy() { super.onDestroy(); // 解除注册 EventBus.getDefault().unregister(this); } }
4、在要发送消息的页面发送消息
发送消息很简单,参数是你自己写的那个类
EventBus.getDefault().post(new MyMessage("this is a message"));
完整代码:
package com.xqx.com.eventbusdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import de.greenrobot.event.EventBus; public class SecondActivity extends Activity{ private Button send_msg; private Button btn_finish; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); send_msg = (Button) findViewById(R.id.send_msg); btn_finish = (Button) findViewById(R.id.btn_finish); send_msg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EventBus.getDefault().post(new MyMessage("this is a message")); } }); btn_finish.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
---------------------------------------------------------------------------------------
EventBus其他知识说明:
1、EventBus有四个不同的消息接收处理方法:
onEvent:
使用onEvent,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:
使用onEventMainThread,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackgroundThread:
使用onEventBackgrondThread,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:
使用onEventAsync,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
2、如果有多个地方发送消息,并且有多个消息处理函数,怎么确定哪个消息处理方法处理哪些消息呢?
这就看四个消息处理方法的参数。发送消息的参数是某一个类,接收的也必须是这个类,否则接收不到。如有有多个OnEvent()方法参数相同,那么这些方法都可以接收到消息。
---------------------------------------------------------------------------------------
总结:
register(注册)会把当前类中匹配的方法(onEvent开头的),存入一个map,而post会根据实参去map查找进行反射调用
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android项目实战(十一):moveTaskToBack(boolean ) 方法的使用
原文: Android项目实战(十一):moveTaskToBack(boolean ) 方法的使用 当你开发的程序被按后退键退出的时候, 你肯定不想让他就这么被finish()吧,那么就想把程序退置到后台就可。 (类似于PC端,你关闭一个浏览器和你最小化一个浏览器的区别) 参看方法:public boolean moveTaskToBack(boolean nonRoot) 参数是一个布尔值 1、false,仅当activity为task根(即首个activity例如启动activity之类的)时才生效 2、true,无论什么时候都会生效,忽略上述条件 注意:此方法不会改变task中的activity中的顺序,效果基本等同于home键 --------------------------------------------------------------------------------------- 使用此方法,便不会执行Activity的onDestroy()方法 这样只有在第一次启动的时候会执行onCreate()。以后(只要进程不死掉)都不会执行onCreate() ...
- 下一篇
Android项目实战(十二):解决OOM的一种偷懒又有效的办法
原文: Android项目实战(十二):解决OOM的一种偷懒又有效的办法 在程序的manifest文件的application节点加入android:largeHeap=“true”即可。 对,只需要一句话! 那么这行代码的意思是什么呢? 简单的说就是使该APP获取最大可分配的内存,以便解决OOM问题、 但是、OOM问题出现的原因总得来说有两点: 1、某个手机的内存真的很少 2、代码问题,比如没有处理好Bitmap图片的大小 可以说,出现OOM的情况基本都是第二种情况,那么就需要修改代码,看看哪里没有处理好。 所以说给应用程序使用:android:largeHeap=“true” 这个方法解决OOM的问题只是一种治标不治本的方式 所以,对于本身对内存要求过大的图片或者视频应用,我们可以使用largeHeap。 有人说使用这行代码会对GC产生影响,也有人说不会。这里博主就确定不了了 ,大家可以上网查看下。 -------------------------------------------------------------------------------------- 最后加...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS7安装Docker,走上虚拟化容器引擎之路
- Eclipse初始化配置,告别卡顿、闪退、编译时间过长
- Mario游戏-低调大师作品
- 2048小游戏-低调大师作品
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Thymeleaf,官方推荐html解决方案
- Red5直播服务器,属于Java语言的直播服务器