zoukankan      html  css  js  c++  java
  • 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     @Override
    21     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     @Override
    29     protected void onDestroy() {
    30         // TODO Auto-generated method stub
    31         super.onDestroy();
    32         EventBus.getDefault().unregister(this);
    33     }
    34     private void setView() {
    35         // TODO Auto-generated method stub
    36         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             @Override
    41             public void onClick(View arg0) {
    42                 // TODO Auto-generated method stub
    43                 startActivity(new Intent(MainActivity.this, OneActivity.class));
    44             }
    45         });
    46     }
    47     private boolean isShow = false;
    48     @Override
    49     protected void onStart() {
    50         // TODO Auto-generated method stub
    51         super.onStart();
    52         isShow = true;
    53     }
    54     @Override
    55     protected void onStop() {
    56         // TODO Auto-generated method stub
    57         super.onStop();
    58         isShow =false;
    59     }
    60     @Subscribe
    61     public void onMessageReviced(final String Message) {
    62         tv_View.setText(Message);
    63         System.out.println("收到消息ddd :" + Message);
    64     }
    65 
    66     @Subscribe
    67     public void onMessageReviced(final Student Message) {
    68         //显示一个弹窗
    69         System.out.println("收到消息dddx :" + Message);
    70         if(Message != null){
    71             showAlertDialog(this, "姓名:" + Message.getName() + "\年龄:" + Message.getAge() + "\性别:" +Message
    72                     .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             @Override
    82             public void onClick(DialogInterface arg0, int arg1) {
    83                 // TODO Auto-generated method stub
    84                 
    85             }
    86         });
    87         AlertDialog mDialog = dialog.create();
    88         mDialog.show();
    89     }
    90 }
  • 相关阅读:
    Linux中的官方源、镜像源汇总
    提示"libc.so.6: version `GLIBC_2.14' not found"
    win8.1 安装msi软件出现 2503、2502
    平均负载(Load average)
    oracle 导入报错:field in data file exceeds maximum length
    一个命令的输出作为另外一个命令的输入
    Http 状态码
    Linux 命令总结
    ORA-12505: TNS: 监听程序当前无法识别连接描述符中所给出的SID等错误解决方法
    轻松应对IDC机房带宽突然暴涨问题
  • 原文地址:https://www.cnblogs.com/wangfengdange/p/5871046.html
Copyright © 2011-2022 走看看