当前位置: 移动技术网 > 移动技术>移动开发>Android > Android之利用EventBus发送消息传递示例

Android之利用EventBus发送消息传递示例

2019年07月24日  | 移动技术网移动技术  | 我要评论

一、概述

eventbus是一款针对android优化的发布/订阅事件总线。主要功能是替代intent,handler,broadcast在fragment,activity,service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。

1、下载eventbus的类库

源码:

2、基本使用

(1)自定义一个类,可以是空类,比如:

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中,获取实例的方法一般是采用eventbus.getinstance()来获取默认的eventbus实例,当然你也可以new一个又一个,个人感觉还是用默认的比较好,以防出错。

二、实战

先给大家看个例子:

当击btn_try按钮的时候,跳到第二个activity,当点击第二个activity上面的first event按钮的时候向第一个activity发送消息,当第一个activity收到消息后,一方面将消息toast显示,一方面放入textview中显示。

按照下面的步骤,下面来建这个工程:

1、基本框架搭建

想必大家从一个activity跳转到第二个activity的程序应该都会写,这里先稍稍把两个activity跳转的代码建起来。后面再添加eventbus相关的玩意。

mainactivity布局(activity_main.xml)

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical"> 
  
 <button 
  android:id="@+id/btn_try" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="btn_bty"/> 
 <textview 
  android:id="@+id/tv" 
  android:layout_width="wrap_content" 
  android:layout_height="match_parent"/> 
 
</linearlayout> 

新建一个activity,secondactivity布局(activity_second.xml)

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.harvic.try_eventbus_1.secondactivity" > 
 
 <button 
  android:id="@+id/btn_first_event" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="first event"/> 
 
</linearlayout> 

mainactivity.java (点击btn跳转到第二个activity)

public class mainactivity extends activity { 
 
 button btn; 
 
 @override 
 protected void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.activity_main); 
 
  btn = (button) findviewbyid(r.id.btn_try); 
 
  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); 
   } 
  }); 
 } 
 
} 

到这,基本框架就搭完了,下面开始按步骤使用eventbus了。

2、新建一个类firstevent

package com.harvic.other; 
 
public class firstevent { 
 
 private string mmsg; 
 public firstevent(string msg) { 
  // todo auto-generated constructor stub 
  mmsg = msg; 
 } 
 public string getmsg(){ 
  return mmsg; 
 } 
} 

这个类很简单,构造时传进去一个字符串,然后可以通过getmsg()获取出来。

3、在要接收消息的页面注册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方法来实现发送的,发送过去的是我们新建的类的实例!

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")); 
   } 
  }); 
 } 
} 

5、接收消息

接收消息时,我们使用eventbus中最常用的oneventmainthread()函数来接收消息,具体为什么用这个,我们下篇再讲,这里先给大家一个初步认识,要先能把eventbus用起来先。

在mainactivity中重写oneventmainthread(firstevent event),参数就是我们自己定义的类:

在收到event实例后,我们将其中携带的消息取出,一方面toast出去,一方面传到textview中;

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(); 
} 

完整的mainactiviy代码如下:

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); 
 } 
} 

好了,到这,基本上算初步把eventbus用起来了,下篇再讲讲eventbus的几个函数,及各个函数间是如何识别当前如何调用哪个函数的。

源码地址:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网