Android之利用EventBus進行消息傳遞

什麼是EventBus

EventBus是一個 發佈/訂閱 模式的消息總線庫,它簡化了應用程序內各組件間、組件與後臺線程間的通訊,解耦了事件的發送者和接收者,避免了複雜的、易於出錯的依賴及生命週期問題,可使咱們的代碼更加簡潔、健壯。EventBus 用於各組件通訊,那麼用於 fragment 之間的通訊就很是合適了。html


 

一、基本框架搭建

想必你們從一個Activity跳轉到第二個Activity的程序應該都會寫,這裏先稍稍把兩個Activity跳轉的代碼建起來。後面再添加EventBus相關的玩意。java

MainActivity佈局(activity_main.xml)
android

 

[html] view plain copy
  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)
app

 

[html] view plain copy
  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)
框架

[java] view plain copy
  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了。
ide

二、新建一個類FirstEvent

[java] view plain copy
  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. }   


三、在要接收消息的頁面註冊EventBus:

在上面的GIF圖片的演示中,你們也能夠看到,咱們是要在MainActivity中接收發過來的消息的,因此咱們在MainActivity中註冊消息。函數

經過咱們會在OnCreate()函數中註冊EventBus,在OnDestroy()函數中反註冊。因此總體的註冊與反註冊的代碼以下:佈局

[java] view plain copy
  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. }    


四、發送消息

發送消息是使用EventBus中的Post方法來實現發送的,發送過去的是咱們新建的類的實例!
post

 

[java] view plain copy
  1. EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));    

完整的SecondActivity.Java的代碼以下:this

 

[java] view plain copy
  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. }    


五、接收消息

接收消息時,咱們使用EventBus中最經常使用的onEventMainThread()函數來接收消息,具體爲何用這個,咱們下篇再講,這裏先給你們一個初步認識,要先能把EventBus用起來先。

在MainActivity中重寫onEventMainThread(FirstEvent event),參數就是咱們本身定義的類:

在收到Event實例後,咱們將其中攜帶的消息取出,一方面Toast出去,一方面傳到TextView中;

[java] view plain copy
    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. }   
相關文章
相關標籤/搜索