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. python_实现发送邮件功能

    #!/usr/bin/env python #-*- coding:utf-8 -*- from email import encoders from email.header import Head ...

  2. bzoj 3343: 教主的魔法

    Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 924  Solved: 402[Submit][Status][Discuss] Descriptio ...

  3. 加法变乘法|2015年蓝桥杯B组题解析第六题-fishers

    加法变乘法 我们都知道:1+2+3+ ... + 49 = 1225 现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如: 1+2+3+...+1011+12+...+2728+29+ ...

  4. 【第二十七章】 springboot + zipkin(brave-okhttp实现)

    本文截取自:http://blog.csdn.net/liaokailin/article/details/52077620 一.前提 1.zipkin基本知识:附8 zipkin 2.启动zipki ...

  5. hdu 6180 Schedule

    Schedule Problem Description There are N schedules, the i-th schedule has start time si and end time ...

  6. BZOJ 1006: [HNOI2008]神奇的国度(弦图染色)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1006 题意: 思路: 这个就是弦图染色问题,弦图啥的反正我也不懂,具体看论文https://wenk ...

  7. NOI 7614 最低通行费(多段图最短路)

    http://noi.openjudge.cn/ch0206/7614/ 题意: 有一个N*N的正方形网格,商人从网格的左上角进,右下角出.每穿越中间1个小方格,都要花费1个单位时间.商人必须在(2N ...

  8. UVa 10285 最长的滑雪路径(DAG上的最长路)

    https://vjudge.net/problem/UVA-10285 题意: 在一个R*C的整数矩阵上找一条高度严格递减的最长路.起点任意,但每次只能沿着上下左右4个方向之一走一格,并且不能走出矩 ...

  9. NYOJ 16 矩形嵌套(经典DP)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=16 矩形嵌套 时间限制:3000 ms  |           内存限制:65535 KB 难度: ...

  10. API接口自动化之3 同一个war包中多个接口做自动化测试

    同一个war包中多个接口做自动化测试 一个接口用一个测试类,每个测试用例如下,比如下面是4个测试用例,每个详细的测试用例中含有请求入参,返回体校验,以此来判断每条测试用例是否通过 一个war包中,若含 ...