首页 > 代码库 > EventBus简单的实现

EventBus简单的实现

EventBus是最近项目用到的,也只是会些简单的功能,不过感觉功能蛮强大的。代码链接:http://download.csdn.net/detail/qq_29774291/9629346

EventBus是Guava的事件处理机制,是设计模式中的观察者模式(生产/消费者编程模型)的优雅实现。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。

一.下载的类库:https://github.com/greenrobot/EventBus

二.基本的使用:

  1.在要接收消息的页面注册:

1 EventBus.getDefault().register(this);

  2.在发送界面发送消息:

Student student = new Student(20, "张三", "男");EventBus.getDefault().post(student);

  3.在接受界面接受信息:

@Subscribe    public void onMessageReviced(final Student Message) {        //显示一个弹窗        System.out.println("收到消息dddx :" + Message);        if(Message != null){            showAlertDialog(this, "姓名:" + Message.getName() + "\\年龄:" + Message.getAge() + "\\性别:" +Message                    .getSex());        }    }

  4.解除注册:

        @Override    protected void onDestroy() {        // TODO Auto-generated method stub        super.onDestroy();        EventBus.getDefault().unregister(this);    }    

主程序代码

 1 package com.item.jiejie.event; 2  3 import de.greenrobot.event.EventBus; 4 import de.greenrobot.event.Subscribe; 5 import android.os.Bundle; 6 import android.app.Activity; 7 import android.app.AlertDialog; 8 import android.content.Context; 9 import android.content.DialogInterface;10 import android.content.Intent;11 import android.view.View;12 import android.widget.Button;13 import android.widget.TextView;14 15 public class MainActivity extends Activity {16 17     private TextView tv_View;18     private Button btn_to_finish;19 20     @Override21     protected void onCreate(Bundle savedInstanceState) {22         super.onCreate(savedInstanceState);23         setContentView(R.layout.activity_main);24         setView();25         EventBus.getDefault().register(this);26     }27     28     @Override29     protected void onDestroy() {30         // TODO Auto-generated method stub31         super.onDestroy();32         EventBus.getDefault().unregister(this);33     }34     private void setView() {35         // TODO Auto-generated method stub36         tv_View = (TextView) findViewById(R.id.tv_text);37         btn_to_finish = (Button) findViewById(R.id.btn_to_one);38         btn_to_finish.setOnClickListener(new View.OnClickListener() {39 40             @Override41             public void onClick(View arg0) {42                 // TODO Auto-generated method stub43                 startActivity(new Intent(MainActivity.this, OneActivity.class));44             }45         });46     }47     private boolean isShow = false;48     @Override49     protected void onStart() {50         // TODO Auto-generated method stub51         super.onStart();52         isShow = true;53     }54     @Override55     protected void onStop() {56         // TODO Auto-generated method stub57         super.onStop();58         isShow =false;59     }60     @Subscribe61     public void onMessageReviced(final String Message) {62         tv_View.setText(Message);63         System.out.println("收到消息ddd :" + Message);64     }65 66     @Subscribe67     public void onMessageReviced(final Student Message) {68         //显示一个弹窗69         System.out.println("收到消息dddx :" + Message);70         if(Message != null){71             showAlertDialog(this, "姓名:" + Message.getName() + "\\年龄:" + Message.getAge() + "\\性别:" +Message72                     .getSex());73         }74     }75     private void showAlertDialog(final Context context,String string){76         AlertDialog.Builder dialog = new AlertDialog.Builder(context);77         dialog.setTitle("我是标题");78         dialog.setMessage(string);79         dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {80             81             @Override82             public void onClick(DialogInterface arg0, int arg1) {83                 // TODO Auto-generated method stub84                 85             }86         });87         AlertDialog mDialog = dialog.create();88         mDialog.show();89     }90 }

 

EventBus简单的实现