android关于EventBus优化Application

来源:互联网 发布:阿里云企业版邮箱 编辑:程序博客网 时间:2024/06/15 23:00

前言:

早上翻看了一下简书,无意看到了一个比较有兴趣的文章,关于APP启动优化的,写的很好,学习了一下,顺便记录一下。

文章地址:http://www.jianshu.com/p/84983a3bdbff

概要:

本文主要介绍EventBus集成到工程中,以及一些简单的使用,不对概念性的内容做太多介绍。

EventBus在SVN的地址:https://github.com/greenrobot/EventBus

正文:

一,集成EventBus

根据文档说明,有两种集成方法:(首先查一下EventBus的最新版本)

1)Gradle:

在app/src/build.gradle中添加:

compile 'org.greenrobot:eventbus:3.0.0'
2Maven:

添加依赖:

<dependency>    <groupId>org.greenrobot</groupId>    <artifactId>eventbus</artifactId>    <version>3.0.0</version></dependency>


二,EventBus的使用方法

盗文档图一张

EventBus:(文档说明)

简化了组件间的通信,解藕了事件的发送者和接收者,很好地作用于Activity,Fragment和后台线程,避免了复杂的依赖关系以及生命周期引发的一些问题,让代码更简单,运行更快。

实现消息的发送接受需要三个步骤:

1)定义对应的事件以及类

public static class MessageEvent { /* Additional fields if needed */ }
2)声明注册订阅方法,并指定线程模式:

@Subscribe(threadMode = ThreadMode.MAIN)  public void onMessageEvent(MessageEvent event) {/* Do something */};
注册,反注册EventBus:

@Overridepublic void onStart() {    super.onStart();    EventBus.getDefault().register(this);}@Overridepublic void onStop() {    super.onStop();    EventBus.getDefault().unregister(this);}
3)发送事件:

EventBus.getDefault().post(new MessageEvent());

三,实现用EventBus控制Application初始化

在启动APP的时候会先创建Application,初始化,然后才进我们的启动页面;这里我们需要在创建Application的时候不执行初始化,先进启动页面,然后通知Application执行初始化。

首先自定义一个MyApplication继承自Application:

public class MyApplication extends Application {    @Override    public void onCreate() {        super.onCreate();        Log.e("MyApplication","-------------------onCreate--------------");    }    public MyApplication(){        Log.e("MyApplication","------------------MyApplication()--------------");    }    public MyApplication(String str){        Log.e("MyApplication","------------------MyApplication(String str)--------------");        init();    }    /*初始化方法*/    public void init(){        Log.e("MyApplication","------------------init()-------------------");    }}
init()里面是我们要调用的初始化方法。在AndroidMainfest.xml配置Application:

<application        android:allowBackup="true"        android:name=".MyApplication"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity"            android:theme="@style/AppBlackTheme">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>
然后到我们的启动页面的Activity里面注册,反注册EventBus,注册订阅方法:

注册

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Log.e("MainActivity","=============onCreate==============");        EventBus.getDefault().register(this);            }

反注册

@Override    protected void onDestroy() {        super.onDestroy();        EventBus.getDefault().unregister(this);    }
订阅方法

@Subscribe(threadMode = ThreadMode.MAIN)    public void onMessageEvent(MyApplication event) {        Log.e("MainActivity","------------------success--------------");    }


然后添加发送调用方法post:

@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Log.e("MainActivity","=============onCreate==============");        EventBus.getDefault().register(this);        EventBus.getDefault().post(new MyApplication(""));    }

现在我们一个流程就完成了,运行一下:

11405-11405/demo.bys.com.testeventbus E/MyApplication: ------------------MyApplication()--------------11405-11405/demo.bys.com.testeventbus E/MyApplication: -------------------onCreate--------------11405-11405/demo.bys.com.testeventbus E/MainActivity: =============onCreate==============11405-11405/demo.bys.com.testeventbus E/MyApplication: ------------------MyApplication(String str)--------------11405-11405/demo.bys.com.testeventbus E/MyApplication: ------------------init()-------------------11405-11405/demo.bys.com.testeventbus E/MainActivity: ------------------success--------------

打开App,先执行了Application的无参初始化方法和onCreate,然后进入启动页,执行Activity的onCreate方法,在onCreate方法里面调用post执行带参数的MyApplication方法,这个时候执行初始化操作,操作完成后,订阅方法会收到消息。

1 0
原创粉丝点击