zoukankan      html  css  js  c++  java
  • Android之利用EventBus进行消息传递

    什么是EventBus

    EventBus是一个 发布/订阅 模式的消息总线库,它简化了应用程序内各组件间、组件与后台线程间的通信,解耦了事件的发送者和接收者,避免了复杂的、易于出错的依赖及生命周期问题,可以使我们的代码更加简洁、健壮。EventBus 用于各组件通信,那么用于 fragment 之间的通信就非常合适了。


     

    1、基本框架搭建

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

    MainActivity布局(activity_main.xml)

     

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    2.     xmlns:tools="http://schemas.android.com/tools"    
    3.     android:layout_width="match_parent"    
    4.     android:layout_height="match_parent"    
    5.     android:orientation="vertical">    
    6.         
    7.     <Button     
    8.         android:id="@+id/btn_try"    
    9.         android:layout_width="match_parent"    
    10.         android:layout_height="wrap_content"    
    11.         android:text="btn_bty"/>    
    12.     <TextView     
    13.         android:id="@+id/tv"    
    14.         android:layout_width="wrap_content"    
    15.         android:layout_height="match_parent"/>    
    16.     
    17. </LinearLayout>   


    新建一个Activity,SecondActivity布局(activity_second.xml)

     

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    2.     xmlns:tools="http://schemas.android.com/tools"    
    3.     android:layout_width="match_parent"    
    4.     android:layout_height="match_parent"    
    5.     android:orientation="vertical"    
    6.     tools:context="com.harvic.try_eventbus_1.SecondActivity" >    
    7.     
    8.     <Button     
    9.         android:id="@+id/btn_first_event"    
    10.         android:layout_width="match_parent"    
    11.         android:layout_height="wrap_content"    
    12.         android:text="First Event"/>    
    13.     
    14. </LinearLayout>    

    MainActivity.java (点击btn跳转到第二个Activity)

    1. public class MainActivity extends Activity {    
    2.     
    3.     Button btn;    
    4.     
    5.     @Override    
    6.     protected void onCreate(Bundle savedInstanceState) {    
    7.         super.onCreate(savedInstanceState);    
    8.         setContentView(R.layout.activity_main);    
    9.     
    10.         btn = (Button) findViewById(R.id.btn_try);    
    11.     
    12.         btn.setOnClickListener(new View.OnClickListener() {    
    13.     
    14.             @Override    
    15.             public void onClick(View v) {    
    16.                 // TODO Auto-generated method stub    
    17.                 Intent intent = new Intent(getApplicationContext(),    
    18.                         SecondActivity.class);    
    19.                 startActivity(intent);    
    20.             }    
    21.         });    
    22.     }    
    23.     
    24. }    


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

    2、新建一个类FirstEvent

    1. package com.harvic.other;    
    2.     
    3. public class FirstEvent {    
    4.     
    5.     private String mMsg;    
    6.     public FirstEvent(String msg) {    
    7.         // TODO Auto-generated constructor stub    
    8.         mMsg = msg;    
    9.     }    
    10.     public String getMsg(){    
    11.         return mMsg;    
    12.     }    
    13. }   


    3、在要接收消息的页面注册EventBus:

    在上面的GIF图片的演示中,大家也可以看到,我们是要在MainActivity中接收发过来的消息的,所以我们在MainActivity中注册消息。

    通过我们会在OnCreate()函数中注册EventBus,在OnDestroy()函数中反注册。所以整体的注册与反注册的代码如下:

    1. package com.example.tryeventbus_simple;    
    2.     
    3. import com.harvic.other.FirstEvent;    
    4.     
    5. import de.greenrobot.event.EventBus;    
    6. import android.app.Activity;    
    7. import android.content.Intent;    
    8. import android.os.Bundle;    
    9. import android.util.Log;    
    10. import android.view.View;    
    11. import android.widget.Button;    
    12. import android.widget.TextView;    
    13. import android.widget.Toast;    
    14.     
    15. public class MainActivity extends Activity {    
    16.     
    17.     Button btn;    
    18.     TextView tv;    
    19.     
    20.     @Override    
    21.     protected void onCreate(Bundle savedInstanceState) {    
    22.         super.onCreate(savedInstanceState);    
    23.         setContentView(R.layout.activity_main);    
    24.                 //注册EventBus    
    25.         EventBus.getDefault().register(this);    
    26.     
    27.         btn = (Button) findViewById(R.id.btn_try);    
    28.         tv = (TextView)findViewById(R.id.tv);    
    29.     
    30.         btn.setOnClickListener(new View.OnClickListener() {    
    31.     
    32.             @Override    
    33.             public void onClick(View v) {    
    34.                 // TODO Auto-generated method stub    
    35.                 Intent intent = new Intent(getApplicationContext(),    
    36.                         SecondActivity.class);    
    37.                 startActivity(intent);    
    38.             }    
    39.         });    
    40.     }    
    41.     @Override    
    42.     protected void onDestroy(){    
    43.         super.onDestroy();    
    44.         EventBus.getDefault().unregister(this);//反注册EventBus    
    45.     }    
    46. }    


    4、发送消息

    发送消息是使用EventBus中的Post方法来实现发送的,发送过去的是我们新建的类的实例!

     

    1. EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));    

    完整的SecondActivity.Java的代码如下:

     

    1. package com.example.tryeventbus_simple;    
    2.     
    3. import com.harvic.other.FirstEvent;    
    4.     
    5. import de.greenrobot.event.EventBus;    
    6. import android.app.Activity;    
    7. import android.os.Bundle;    
    8. import android.view.View;    
    9. import android.widget.Button;    
    10.     
    11. public class SecondActivity extends Activity {    
    12.     private Button btn_FirstEvent;    
    13.     
    14.     @Override    
    15.     protected void onCreate(Bundle savedInstanceState) {    
    16.         super.onCreate(savedInstanceState);    
    17.         setContentView(R.layout.activity_second);    
    18.         btn_FirstEvent = (Button) findViewById(R.id.btn_first_event);    
    19.     
    20.         btn_FirstEvent.setOnClickListener(new View.OnClickListener() {    
    21.     
    22.             @Override    
    23.             public void onClick(View v) {    
    24.                 // TODO Auto-generated method stub    
    25.                 EventBus.getDefault().post(    
    26.                         new FirstEvent("FirstEvent btn clicked"));    
    27.             }    
    28.         });    
    29.     }    
    30. }    


    5、接收消息

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

    在MainActivity中重写onEventMainThread(FirstEvent event),参数就是我们自己定义的类:

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

      1. public void onEventMainThread(FirstEvent event) {    
      2.     
      3.     String msg = "onEventMainThread收到了消息:" + event.getMsg();    
      4.     Log.d("harvic", msg);    
      5.     tv.setText(msg);    
      6.     Toast.makeText(this, msg, Toast.LENGTH_LONG).show();    
      7. }   
  • 相关阅读:
    tensflow安装
    Dubbo的服务注册--Zookeeper
    Dubbo源码分析之Exporter---服务暴露(本地和远程)
    Dubbo源码分析之XML的Bean解析
    Dubbo的SPI可扩展机制的源码分析
    Dubbo源码分析(三)-----消费者引用服务启动时序
    导出mysql的表结构的字段为excel
    Dubbo源码分析(二)-----提供者启动过程分析
    dubbo的api的配置(基于注解的配置)
    Dubbo源码分析(一)-----包结构的分析
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/7573479.html
Copyright © 2011-2022 走看看