github原文java
EventBus...
* simplifies the communication between components
- decouples event senders and receivers
- performs well with Activities, Fragments, and background threads
- avoids complex and error-prone dependencies and life cycle issues
- makes your code simpler
* is fast
* is tiny (<50k jar)
* is proven in practice by apps with 100,000,000+ installs
* has advanced features like delivery threads, subscriber priorities, etc.
大概意思android
EventBus...
* 簡化組件之間的通訊
- 發送者和接收者解耦
- 能夠在Activities, Fragments,background threads(服務、子線程等)之間傳遞數據
- 避免了依賴和生命週期等容易出錯的問題
- 讓你的程序看上去更簡潔
* 更快
* 更小(jar包大小小於50k)
* 已經有這麼這麼多應用用過了EventBus。
* 有一些更高級的功能...
看過簡介,應該大概知道EventBus是一個什麼東西了吧git
源碼地址:https://github.com/greenrobot/EventBusgithub
API文檔地址:https://github.com/greenrobot/EventBus/blob/master/HOWTO.mdmarkdown
首先要導入jar包,這個就不用多說了,而後要有一個發送者發送一個消息,最後要有一個接收者接收發送過來的消息網絡
所以,在你要給某個接收者發送消息的時候,必定要保證接收者已經註冊了,否者接收者都尚未,誰來接收呢。併發
爲何提到這個呢,由於我剛用的時候,我是想在一個Activity1裏開啓另外一個Activity2,而後用EventBus傳遞過去一個數據,因而我發送了一個消息,而後用Intent開啓另一個Activity2,在Activity2裏註冊了接收者,可是卻沒有收到消息,以後我又先開啓Activity2,等初始化好了之後,再發送消息,Activity2就收到消息了app
因此想一下EventBus的應用場景,若是咱們有一個服務要在後臺一直刷數據,而後在界面上顯示,相似這種場景,咱們就能夠用EventBus來實現異步
普通的頁面跳轉須要傳遞的數據,咱們用Intent就徹底能夠啦。 async
在Gradle文件里加上jar包
很是簡單,就一行代碼
EventBus.getDefault().post(new MessageEvent("Hello everyone!"));
這裏須要傳遞一個數據對象,就是一個普通的Javabean
public class MessageEvent {
public final String message;
public MessageEvent(String message) {
this.message = message;
}
}
註冊接收者
EventBus.getDefault().register(this);
反註冊接收者
EventBus.getDefault().unregister(this);
四個接收方法
// Called in the same thread (default)
public void onEvent(MessageEvent event) {
log(event.message);
}
// Called in Android UI's main thread
public void onEventMainThread(MessageEvent event) {
textField.setText(event.message);
}
// Called in the background thread
public void onEventBackgroundThread(MessageEvent event){
saveToDisk(event.message);
}
// Called in a separate thread
public void onEventAsync(MessageEvent event){
backend.send(event.message);
}
後臺一個Service刷數據,頁面顯示數據
下載地址(Android Studio工程):http://download.csdn.net/detail/q4878802/9057545
package com.kongqw.kqweventbusdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.kongqw.kqweventbusdemo.bean.MessageEvent;
import com.kongqw.kqweventbusdemo.service.KqwService;
import de.greenrobot.event.EventBus;
public class MainActivity extends Activity {
private TextView mTvShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 顯示信息的TextView
mTvShow = (TextView) findViewById(R.id.tv_show);
// 開啓服務 刷數據
Intent service = new Intent(this, KqwService.class);
startService(service);
}
@Override
public void onStart() {
EventBus.getDefault().register(this);
super.onStart();
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
// Called in Android UI's main thread
public void onEventMainThread(MessageEvent event) {
mTvShow.setText("onEventMainThread : \n" + event.message);
}
}
package com.kongqw.kqweventbusdemo.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import com.kongqw.kqweventbusdemo.bean.MessageEvent;
import de.greenrobot.event.EventBus;
public class KqwService extends Service {
public KqwService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
EventBus.getDefault().post(new MessageEvent("Service 傳遞過來的數據"));
/* * 模擬網絡刷新數據 */
new Thread() {
@Override
public void run() {
for (int x = 0; x < Integer.MAX_VALUE; x++) {
try {
sleep(1000);
EventBus.getDefault().post(new MessageEvent("Service 傳遞過來的數據 : " + x));
long id = Thread.currentThread().getId();
Log.d("KqwService", "Service發送了數據:" + x + "線程ID : " + id);
} catch (Exception e) {
// TODO
Log.d("KqwService", "error :" + e);
}
}
}
}.start();
}
}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示數據"
android:textSize="25dp" />
</RelativeLayout>
// This method will be called when a MessageEvent is posted
public void onEvent(MessageEvent event) {
long id = Thread.currentThread().getId();
Log.d("onEventMainThread", "Thread id = " + id);
mTvShow.setText("onEvent : " + event.message);
}
// Called in Android UI's main thread
public void onEventMainThread(MessageEvent event) {
long id = Thread.currentThread().getId();
Log.d("onEventMainThread", "Thread id = " + id);
mTvShow.setText("onEventMainThread : \n" + event.message);
}
// Called in the background thread
public void onEventBackgroundThread(final MessageEvent event){
long id = Thread.currentThread().getId();
Log.d("onEventBackgroundThread", "Thread id = " + id);
runOnUiThread(new Runnable() {
@Override
public void run() {
mTvShow.setText("onEventBackgroundThread : " + event.message);
}
});
}
// Called in a separate thread
public void onEventAsync(final MessageEvent event) {
long id = Thread.currentThread().getId();
Log.d("onEventAsync", "Thread id = " + id);
runOnUiThread(new Runnable() {
@Override
public void run() {
mTvShow.setText("onEventAsync : " + event.message);
}
});
}