Android EventBus 详解(二)

来源:互联网 发布:mac词典设置 编辑:程序博客网 时间:2024/06/08 15:16

上篇介绍了EventBus的简单知识 此篇详细介绍EventBus


EventBus jar包下载地址:http://download.csdn.net/detail/weixin_37730482/9818976


EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息。


优点是开销小,代码更优雅。以及将发送者和接收者解耦。


EventBus 主要的几个方法

1.注册:

eventBus.register(this); 

2.发送消息

eventBus.post(new AnyEventType event); 

3.接受消息四个方法

public void onEvent(AnyEventType event) {}

public void onEventMainThread(AnyEventType event) {}

public void onEventBackgroundThread(AnyEventType event) {}

public void onEventAsync(AnyEventType event) {}


这四种订阅函数都是使用onEvent开头的,它们的功能稍有不同,在介绍不同之前先介绍两个概念:


告知观察者事件发生时通过EventBus.post函数实现,这个过程叫做事件的发布,观察者被告知事件发生叫做事件的接收,是通过下面的订阅函数实现的。


onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。


onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。


onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。


onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.


4.注销

eventBus.unregister(this);



以下是示例demo

就是从一个页面跳转到另一个页面 然后用EventBus传递值更新第一个页面数据


第一个页面xml

<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"
    tools:context="com.example.eventbusdemo.MainActivity$PlaceholderFragment" >


    <Button 
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="跳转"/>
    
    <TextView
        android:id="@+id/textview1"
        android:layout_below="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />


</RelativeLayout>


第一个页面java

package com.example.eventbusdemo;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
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 Button button;
private TextView textview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册EventBus
EventBus.getDefault().register(this); 

button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
textview=(TextView) findViewById(R.id.textview1);
textview.setText("初始姓名:"+"张三");
}

public void onEventMainThread(People p) {
textview.setText("EventBus接收姓名:"+p.getName());
}

/**
* onDestroy方法
* */

@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);//注销EventBus 
}


}


第二个页面xml

<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"
    tools:context="com.example.eventbusdemo.MainActivity$PlaceholderFragment" >


    <Button 
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="返回"/>
    
    


</RelativeLayout>


第二个页面java

package com.example.eventbusdemo;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import de.greenrobot.event.EventBus;


public class OtherActivity extends Activity{


private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
button=(Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
//发送消息
People p=new People();
p.setName("李四");
EventBus.getDefault().post(p);//post(Object ob)
}
});
}

}


实体类

package com.example.eventbusdemo;


import java.io.Serializable;


public class People implements Serializable{


private static final long serialVersionUID = 1L;

private String name;


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


}


效果

EventBus 通讯前



EventBus 通讯后



上面说过EventBus接收的接收方法有四个 那么怎么区分呢 除了运行在不同线程中 其实没有什么区别

可以根据传递值进行区分

例如

接收java

package com.example.eventbusdemo;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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 Button button;
private TextView textview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册EventBus
EventBus.getDefault().register(this); 

button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
textview=(TextView) findViewById(R.id.textview1);
textview.setText("初始姓名:"+"张三");
}

public void onEventMainThread(People p) {
textview.setText("EventBus接收姓名:"+p.getName());
Log.d("MainActivity", "onEventMainThread接收----:"+p.getName());
}

public void onEvent(People p){
Log.d("MainActivity", "onEvent接收----:"+p.getName());
}

public void onEventBackgroundThread(People p){
Log.d("MainActivity", "onEventBackgroundThread接收----:"+p.getName());
}

public void onEventAsync(People p){
Log.d("MainActivity", "onEventAsync接收----:"+p.getName());
}

/**
* onDestroy方法
* */

@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);//注销EventBus 
}


}


发送

//发送消息
People p=new People();
p.setName("李四");
EventBus.getDefault().post(p);//post(Object ob)


由于发送传递的实体类是people

接收页面中四个方法全部都是people

四个方法都会接收



接收页面

package com.example.eventbusdemo;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
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 Button button;
private TextView textview;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册EventBus
EventBus.getDefault().register(this); 

button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
startActivity(intent);
}
});
textview=(TextView) findViewById(R.id.textview1);
textview.setText("初始姓名:"+"张三");
}

public void onEventMainThread(People p) {
textview.setText("EventBus接收姓名:"+p.getName());
Log.d("MainActivity", "onEventMainThread接收----:"+p.getName());
}

public void onEvent(People p){
Log.d("MainActivity", "onEvent接收----:"+p.getName());
}

public void onEventBackgroundThread(People p){
Log.d("MainActivity", "onEventBackgroundThread接收----:"+p.getName());
}

public void onEventAsync(People1 p){
Log.d("MainActivity", "onEventAsync接收----:"+p.getName());
}

/**
* onDestroy方法
* */

@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);//注销EventBus 
}


}


即将

public void onEventAsync(People1 p){
Log.d("MainActivity", "onEventAsync接收----:"+p.getName());
}

中实体对象参数改成people1


发送

//发送消息
People p=new People();
p.setName("李四");
EventBus.getDefault().post(p);//post(Object ob)


由于发送传递的实体类是people

接收页面中前三个方法全部都是people

前三个方法都会接收

而第四个方法时people1 所以接收不到



0 0
原创粉丝点击