EventBus是github上的一个第三方开发库,其在github上的项目主页地址:https://github.com/greenrobot/EventBus
EventBus的消息模型是消息发布者/订阅者机制。

(1)EventBus是消息发布者(发送消息)/订阅者(接收消息)模式。EventBus的消息发布十分灵活,可以在工程代码中的任意位置发送消息,EventBus 发布消息只需要一行代码即可实现: EventBus.getDefault().post(event); Event即为自己定义的类的实例。

(2)EventBus在接收消息的Activity(或Fragment)中初始化。通常在Android的Activity(或者Fragment)的onCreate里面注册,仅需一行代码: EventBus.getDefault().register(this); 类似于注册一个消息监听Listener,完了不要忘记注销EventBus,在onDestory里面 EventBus.getDefault().unregister(this);

(3)EventBus接收消息。 在Activity中根据代码实际情况写一个EventBus的消息接收函数: public void onEventMainThread(MyEvent event); 然后,只要EventBus发送消息,就可以在这里接收到。

EventBus的消息回调接收消息函数还有几个: onEventMainThread:Main线程,这个与Android UI线程密切相关,不要阻塞它! onEventBackgroundThread:故名思议,后台线程中接收处理。 onEventAsync:异步线程中接收处理。

 package com.lixu.chuandi;

 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 {
TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv1);
Button button = (Button) findViewById(R.id.btn1);
EventBus.getDefault().register(this);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
Event event = new Event();
event.a = "你好啊 我是:";
event.b = "小新"; EventBus.getDefault().post(event);
Intent intent = new Intent(MainActivity.this, MyAppService.class);
startService(intent); }
}); } // 这里,EventBus回调接受消息,然后更新Main UI的TextView
public void onEventMainThread(Event event) { tv.setText(event.toString());
} @Override
protected void onDestroy() { super.onDestroy();
EventBus.getDefault().unregister(this);
Intent intent = new Intent(MainActivity.this, MyAppService.class);
stopService(intent);
}
}
 package com.lixu.chuandi;

 import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import de.greenrobot.event.EventBus; public class MyAppService extends Service {
@Override
public void onCreate() {
super.onCreate();
EventBus.getDefault().register(this);
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
Event event = new Event();
// 可以随意在工程中的任意位置发送消息给接受者
EventBus.getDefault().post(event.toString());
return super.onStartCommand(intent, flags, startId);
} @Override
public IBinder onBind(Intent intent) {
return null;
} // 在后台中接收消息
public void onEventBackgroundThread(Event event) { Log.e("MyAppService收到消息:", event.toString());
}
}
 package com.lixu.chuandi;

 public class Event {
public String a;
public String b;
@Override
public String toString() {
return a+b;
} }

Android消息通信 第三方开源项目EventBus 的用法的更多相关文章

  1. Android二维码开源项目zxing用例简化和生成二维码、条形码

    上一篇讲到:Android二维码开源项目zxing编译,编译出来后有一个自带的測试程序:CaptureActivity比較复杂,我仅仅要是把一些不用的东西去掉,用看起来更方便,二维码和条形码的流行性自 ...

  2. Android Hawk数据库 github开源项目

    Android Hawk数据库 github开源项目 Hawk 是一个很便捷的数据库  . 操作数据库仅仅需一行代码 , 能存不论什么数据类型 . github 地址: https://github. ...

  3. android两种基本联网方式与一种第三方开源项目的使用

    安卓请求网络的三种方式 在请求网络的时候一般常用的提交方式是post或者get请求,post请求安全,传输大小无限制,但是代码量多些,get请求是浏览器有大小限制,用户提交的信息在浏览器的地址栏显示出 ...

  4. 工业通信的开源项目 HslCommunication 介绍

    前言: 本项目的孵化说来也是机缘巧合的事,本人于13年杭州某大学毕业后去了一家大型的国企工作,慢慢的走上了工业软件,上位机软件开发的道路.于14年正式开发基于windows的软件,当时可选的技术栈就是 ...

  5. Android的SwipeToDismiss第三方开源框架模拟QQ对话列表侧滑删除,置顶,将头像图片圆形化处理。

      <Android SwipeToDismiss:左右滑动删除ListView条目Item> Android的SwipeToDismiss是github上一个第三方开源框架(github ...

  6. Android笔记——导入Github开源项目CircleRefreshLayout

    百度n久都找不到android studio导入第三方类库的正确方法,纠结睡不着 ,最后终于蒙到了方法,原来想太多了  ---------------------------------------- ...

  7. Android开发UI之开源项目第一篇——个性化控件(View)篇

    原文:http://blog.csdn.net/java886o/article/details/24355907 本文为那些不错的Android开源项目第一篇——个性化控件(View)篇,主要介绍A ...

  8. Android非常实用的开源项目框架

    我将文章中所描述的项目都集成在一个apk中,可以直接运行查看效果,2.2以上的机器都可以运行.因为不让直接上传apk文件,我压缩成了zip包 1. Universal-Image-Loader 实现异 ...

  9. android最火的开源项目

    原文地址:http://www.csdn.net/article/2013-05-21/2815370-Android-open-source-projects-finale 此前,CSDN移动频道推 ...

随机推荐

  1. 【集群搭建】Zookeeper集群环境配置

    1.下载解压安装文件 2.配置文件:conf/zoo.cfg tickTime=2000 dataDir=/usr/sunny/logs/zookeeper/data dataLogDir=/usr/ ...

  2. js文字转移效果

    这个例子算是有点样子的. 思路: 字符串操作.左框里面先是预设的.点击按钮时截取左框中的字符串的前一个字符到右框里的字符串后面,以此循环.点击按钮时按钮变为灰色,在循环完成后恢复.计数的总数(右边)是 ...

  3. Linux command line exercises for NGS data processing

    by Umer Zeeshan Ijaz The purpose of this tutorial is to introduce students to the frequently used to ...

  4. go 并发 demo

    两个进程执行两个goroutine // This sample program demonstrates how to create goroutines and // how the schedu ...

  5. shell printf

    printf 可以格式化字符串,还可以制定字符串的宽度.左右对齐方式等.默认 printf 不会像 echo 自动添加换行符,我们可以手动添加 \n. 例子: $ echo "Hello, ...

  6. Java Selenium 笔记

    目录一.基本语句 1.循环控制(break,continue) 3.字符的替换(replace,repalceFirst,replaceAll,regex) 4.字符串的连接("+" ...

  7. TDD、BDD、DDD

    TDDTest-Driven DevelopmentTest-Driven Development (TDD) is a software development technique where au ...

  8. 常用 对象检测 api

    isPrototypeOf()    判断某个 proptotype 对象和某个实例之间的关系 alert(Cat.prototype.isPrototypeOf(cat1)); //true ale ...

  9. C++学习笔记(二)——交换函数(swap)

    这次我们要透过一个简单的函数swap深入理解函数传参的本质以及在C++中如何选择传参方式. 先来看第一段程序: void swap(int x, int y) { int temp = y; y = ...

  10. python 函数参数介绍

    python 函数参数介绍 python 使用过程总,总会遇到 *args,**kw形式的参数,总是一头雾水,而且网上介绍的或是叫法不一,为此专门深入实践进而了解了函数参数的使用 具体请看代码 #-* ...