我的Android入门之路:二、EventBus学习使用

来源:互联网 发布:童装淘宝摄影入门 编辑:程序博客网 时间:2024/05/09 19:54

概述及基本概念

EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间、组件与后台线程间的通信。比如请求网络,等网络返回时通过Handler或Broadcast通知UI,两个Fragment之间需要通过Listener通信,这些需求都可以通过EventBus实现。

作为一个消息总线,有三个主要的元素:

Event:事件
Event可以是任意类型的对象。

Subscriber:事件订阅者,接收特定的事件
在EventBus中,使用约定来指定事件订阅者以简化使用。即所有事件订阅都都是以onEvent开头的函数,具体来说,函数的名字是onEvent,onEventMainThread,onEventBackgroundThread,onEventAsync这四个,这个和ThreadMode有关,后面再说。

Publisher:事件发布者,用于通知Subscriber有事件发生
可以在任意线程任意位置发送事件,直接调用EventBus的post(Object)方法,可以自己实例化EventBus对象,但一般使用默认的单例就好了:EventBus.getDefault(),根据post函数参数的类型,会自动调用订阅相应类型事件的函数。

ThreadMode
前面说了,Subscriber函数的名字只能是那4个,因为每个事件订阅函数都是和一个ThreadMode相关联的,ThreadMode指定了会调用的函数。有以下四个ThreadMode:

PostThread:事件的处理在和事件的发送在相同的进程,所以事件处理时间不应太长,不然影响事件的发送线程,而这个线程可能是UI线程。对应的函数名是onEvent。
MainThread: 事件的处理会在UI线程中执行。事件处理时间不能太长,这个不用说的,长了会ANR的,对应的函数名是onEventMainThread。
BackgroundThread:事件的处理会在一个后台线程中执行,对应的函数名是onEventBackgroundThread,虽然名字是BackgroundThread,事件处理是在后台线程,但事件处理时间还是不应该太长,因为如果发送事件的线程是后台线程,会直接执行事件,如果当前线程是UI线程,事件会被加到一个队列中,由一个线程依次处理这些事件,如果某个事件处理时间太长,会阻塞后面的事件的派发或处理。
Async:事件处理会在单独的线程中执行,主要用于在后台线程中执行耗时操作,每个事件会开启一个线程(有线程池),但最好限制线程的数目。
根据事件订阅都函数名称的不同,会使用不同的ThreadMode,比如果在后台线程加载了数据想在UI线程显示,订阅者只需把函数命名为onEventMainThread。

简单使用

基本的使用步骤就是如下4步。

定义事件类型:
public class MyEvent {}
定义事件处理方法:
public void onEventMainThread
注册订阅者:
EventBus.getDefault().register(this)
发送事件:
EventBus.getDefault().post(new MyEvent())

(以上介绍转自AngelDevil的博客:http://www.cnblogs.com/angeldevil/p/3715934.html)

以下为我自己的相关代码,具体内容是在SecondActivity中用EventBus将操作在主线程、非主线程、当前线程以及异步中进行演示,通过MainActivity里的TextView及后台打印来展示操作结果:

首先是MainActivity

package org.yijing.testeventbus;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.SystemClock;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import de.greenrobot.event.EventBus;public class MainActivity extends Activity {    private TextView showText;    private Button toSecond;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //注册        EventBus.getDefault().register(this);        showText = (TextView)findViewById(R.id.showText);        toSecond = (Button)findViewById(R.id.toSecond);        //跳转到第二个Activity进行Post操作        toSecond.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                Intent intent=new Intent(getApplicationContext(), SecondActivity.class);                startActivity(intent);            }        });    }    //获得当前线程名    public String ThreadName(){        return Thread.currentThread().getName();    }    //将MainEvent对象在MainThread里执行    //将TextView内容进行相应更改操作,并在后台打印当前线程名    public void onEventMainThread(MainEvent event)    {        showText.setText(event.getMsg() + ",Thread in MainEvent:" + ThreadName());        System.out.println("Thread in MainEvent:" + ThreadName());    }    //将BackgroundEvent对象在BackgroundThread里执行    //在后台打印当前线程名    public void onEventBackgroundThread(BackgroundEvent event)    {        System.out.println("Thread in BackgroundEvent:" + ThreadName());    }    //将AsyncEvent对象在Async里执行    //在后台倒数10秒并打印,然后在后台打印当前线程名    public void onEventAsync(AsyncEvent event)    {        for(int i=event.getMax();i>0;i--)        {            System.out.println("" + i);            SystemClock.sleep(1000);        }        System.out.println("Thread in Async:" + ThreadName());    }    //将PostEvent对象在当前线程里执行    //将TextView内容进行相应操作,并在后台打印当前线程名    public void onEvent(PostEvent event)    {        if (event.getMsg() != null) {            showText.setText(event.getMsg() + ",Thread in PostEvent:" + ThreadName());        }        System.out.println("Thread in PostEvent:" + ThreadName());    }    @Override    protected void onDestroy() {        super.onDestroy();        //注销        EventBus.getDefault().unregister(this);    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }}

具体每部分操作的内容都已在代码中注释,要注意的是在每一个有声明EventBus响应函数(即以onEvent+”ThreadMode“命名的函数)的类里都要调用register和unregister进行注册注销,不然将无法正常调用相关函数。
每个onEvent函数,以”onEvent“开头方便注册注销,以四种ThreadMode来标明在哪种线程里执行,以参数类型来确定调用哪个函数。
在MainActivity里,我主要是用一个TextView来进行相关的显示,以及一个按钮跳转到SecondActivity里进行相关的操作。

接下来是SecondActivity,也是本工程中进行具体操作的Activity:

package org.yijing.testeventbus;import de.greenrobot.event.EventBus;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class SecondActivity extends Activity {    private Button button1,button2,button3,button4,button5,button6;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);        button1 = (Button)findViewById(R.id.mainButton);        button2 = (Button)findViewById(R.id.mainButton2);        button3 = (Button)findViewById(R.id.backgroundButton);        button4 = (Button)findViewById(R.id.aysncButton);        button5 = (Button)findViewById(R.id.postButton1);        button6 = (Button)findViewById(R.id.postButton2);        //点击按钮触发对应Event,后台打印post前后所在线程        button1.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                System.out.println("Thread before post:" + Thread.currentThread().getName());                EventBus.getDefault().post(new MainEvent("MainEvent clicked"));                finish();            }        });        button2.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                new Thread(new Runnable() {                    @Override                    public void run() {                        System.out.println("Thread before post:" + Thread.currentThread().getName());                        EventBus.getDefault().post(new MainEvent("New thread MainEvent clicked"));                    }                }).start();                finish();            }        });        button3.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                System.out.println("Thread before post:" + Thread.currentThread().getName());                EventBus.getDefault().post(new BackgroundEvent("BackgroundEvent clicked"));                finish();            }        });        button4.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                System.out.println("Thread before post:" + Thread.currentThread().getName());                EventBus.getDefault().post(new AsyncEvent(10));                finish();            }        });        button5.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                System.out.println("Thread before post:" + Thread.currentThread().getName());                EventBus.getDefault().post(new PostEvent("PostEvent clicked"));                finish();            }        });        button6.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                new Thread(new Runnable() {                    @Override                    public void run() {                        try {                            Thread.sleep(3000);                        } catch (InterruptedException e) {                        }                        System.out.println("Thread before post:" + Thread.currentThread().getName());                        EventBus.getDefault().post(new PostEvent());                    }                }).start();                finish();            }        });    }}

在SecondActivity里由于没有构造onEvent函数,不需要注册注销,直接以post来调用执行即可。
六个按钮依次为了演示:
1.在当前为主线程时调用MainThread的函数,看其是否在主线程里被调用;
2.在一个新线程里调用MainThread的函数,看其是否在主线程里被调用;
3.在主线程里调用BackgroundThread的函数,看其是否在一个新线程里被调用;
4.在主线程里调用Async的函数,看其是否被异步调用(以倒计时的形式让Async函数执行较长一段时间,与此同时点击其他按钮,查看后台打印信息);
5.在主线程里调用PostThread的函数,看其是否在主线程里被调用;
6.在一个新线程里调用PostThread的函数,看其是否在当前线程里被调用;

接下来是所有Event类的构造,对应每个onEvent函数的传入参数:

MainEvent

package org.yijing.testeventbus;public class MainEvent {    private String mMsg;    public MainEvent(String msg) {        mMsg = msg;    }    public String getMsg(){        return mMsg;    }}

BackgroundEvent

package org.yijing.testeventbus;public class BackgroundEvent {    private String mMsg;    public BackgroundEvent(String msg) {        mMsg = msg;    }    public String getMsg(){        return mMsg;    }}

AsyncEvent

package org.yijing.testeventbus;public class AsyncEvent {    private int max;    public AsyncEvent(int max)    {        this.max=max;    }    public int getMax()    {        return max;    }}

PostEvent

package org.yijing.testeventbus;public class PostEvent {    private String mMsg;    public PostEvent() {        mMsg = null;    }    public PostEvent(String msg) {        mMsg = msg;    }    public String getMsg(){        return mMsg;    }}

在本工程中,Event类只用作最简单的参数传递。

在一个工程中,如果想在一个非UI线程里想修改UI线程里的内容,必须通过handle相关操作来传递数据到UI线程里进行修改,一系列操作相对而言还是挺麻烦的。采用EventBus之后,将操作简化,不用顾虑太多如何使用handle,只要在声明响应函数的时候注明ThreadMode即可,是个很不错的便利。

最后再献上学习过程中的相关参考文献并表示感谢:
官网:http://greenrobot.github.io/EventBus/
https://github.com/greenrobot/EventBus
快速Android开发系列通信篇之EventBus:http://www.cnblogs.com/angeldevil/p/3715934.html
Android组件间通信库EventBus学习:http://blog.csdn.net/djun100/article/details/23762621
Android EventBus实战 没听过你就out了:http://blog.csdn.net/lmj623565791/article/details/40794879

0 0
原创粉丝点击