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