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. C# 将 Stream 写入文件

    public void StreamToFile(Stream stream,string fileName) { // 把 Stream 转换成 byte[] byte[] bytes = new ...

  2. ubuntu查询某个库的相关情况

    环境:Ubuntu 14.04 64bit 1.如:查询libjpeg库的位置 ldconfig -p |grep libjpeg 2.如:查询libjpeg库的相关名称 dpkg -l '*jpeg ...

  3. UVa 10118 免费糖果(记忆化搜索+哈希)

    https://vjudge.net/problem/UVA-10118 题意: 桌上有4堆糖果,每堆有N颗.佳佳有一个最多可以装5颗糖的小篮子.他每次选择一堆糖果,把最顶上的一颗拿到篮子里.如果篮子 ...

  4. Codeforces Beta Round #94 div 2 B

    B. Students and Shoelaces time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  5. python 打包成tar包

    def make_targz(output_filename, source_dir): with tarfile.open(output_filename, "w:gz") as ...

  6. ubuntu14.04上 nginx启动停止

    sudo service nginx stop  停止 sudo nginx   启动

  7. API接口自动化之2 处理http请求的返回体,对返回体做校验

    举例一个接口测试的常见流程 1) 发送接口请求2) 断言接口响应状态是不是200 OK3) 断言接口的响应时间低于某一个值(看情况,不是必选)4) 断言响应数据是否正确,一般的做法是判断某一个值是否相 ...

  8. OpenGL遮挡查询

    转自:http://www.cnblogs.com/mazhenyu/p/5083026.html 在一个场景中,如果有有些物体被其他物体遮住了不可见.那么我们就不需要绘制它.在复杂的场景中,这可以减 ...

  9. 对 Kotlin 与 Java 编程语言的思考

    从长远来看,排名前10的也基本上是Java.C.C++.Python.C#.VB.PHP.JavaScript.至于Kotlin的排名,11月份在编程语言仅排41名,Ratings仅有0.216%. ...

  10. C# 中的集合(Array/ArrayList/List<T>/HashTable/Dictionary)

    int [] numbers = new int[5]; // 长度为5,元素类型为 int. string[,] names = new string[5,4]; // 5*4 的二维数组 byte ...