EventBus的四種方法
onEvent:若是使用onEvent做爲訂閱函數,那麼該事件在哪一個線程發佈出來的,onEvent就會在這個線程中運行,也就是說發佈事件和接收事件線程在同一個線程。使用這個方法時,在onEvent方法中不能執行耗時操做,若是執行耗時操做容易致使事件分發延遲。
onEventMainThread:若是使用onEventMainThread做爲訂閱函數,那麼不論事件是在哪一個線程中發佈出來的,onEventMainThread都會在UI線程中執行,接收事件就會在UI線程中運行,這個在Android中是很是有用的,由於在Android中只能在UI線程中跟新UI,因此在onEvnetMainThread方法中是不能執行耗時操做的。
onEventBackground:若是使用onEventBackgrond做爲訂閱函數,那麼若是事件是在UI線程中發佈出來的,那麼onEventBackground就會在子線程中運行,若是事件原本就是子線程中發佈出來的,那麼onEventBackground函數直接在該子線程中執行。
onEventAsync:使用這個函數做爲訂閱函數,那麼不管事件在哪一個線程發佈,都會建立新的子線程在執行onEventAsync.java
EventBus是一款針對Android優化的發佈/訂閱事件總線。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,線程之間傳遞消息.優勢是開銷小,代碼更優雅。以及將發送者和接收者解耦。android
compile 'org.greenrobot:eventbus:3.0.0'
先給你們看個例子:app
當擊btn_try按鈕的時候,跳到第二個Activity,當點擊第二個activity上面的First Event按鈕的時候向第一個Activity發送消息,當第一個Activity收到消息後,一方面將消息Toast顯示,一方面放入textView中顯示。框架
想必你們從一個Activity跳轉到第二個Activity的程序應該都會寫,這裏先稍稍把兩個Activity跳轉的代碼建起來。後面再添加EventBus相關的玩意。ide
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)post
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); } }); } }
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; } }
在上面的GIF圖片的演示中,你們也能夠看到,咱們是要在MainActivity中接收發過來的消息的,因此咱們在MainActivity中註冊消息。優化
經過咱們會在OnCreate()函數中註冊EventBus,在OnDestroy()函數中反註冊。因此總體的註冊與反註冊的代碼以下:this
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 } }
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")); } }); } }
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); } }
參考來源http://blog.csdn.net/harvic880925/article/details/40660137