android EventBus

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

 

二、新建一个类FirstEvent

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

 

三、在要接收消息的页面注册EventBus:

在上面的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

相关文章
相关标签/搜索