Android消息通信 第三方开源项目EventBus 的用法
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 的用法的更多相关文章
- Android二维码开源项目zxing用例简化和生成二维码、条形码
上一篇讲到:Android二维码开源项目zxing编译,编译出来后有一个自带的測试程序:CaptureActivity比較复杂,我仅仅要是把一些不用的东西去掉,用看起来更方便,二维码和条形码的流行性自 ...
- Android Hawk数据库 github开源项目
Android Hawk数据库 github开源项目 Hawk 是一个很便捷的数据库 . 操作数据库仅仅需一行代码 , 能存不论什么数据类型 . github 地址: https://github. ...
- android两种基本联网方式与一种第三方开源项目的使用
安卓请求网络的三种方式 在请求网络的时候一般常用的提交方式是post或者get请求,post请求安全,传输大小无限制,但是代码量多些,get请求是浏览器有大小限制,用户提交的信息在浏览器的地址栏显示出 ...
- 工业通信的开源项目 HslCommunication 介绍
前言: 本项目的孵化说来也是机缘巧合的事,本人于13年杭州某大学毕业后去了一家大型的国企工作,慢慢的走上了工业软件,上位机软件开发的道路.于14年正式开发基于windows的软件,当时可选的技术栈就是 ...
- Android的SwipeToDismiss第三方开源框架模拟QQ对话列表侧滑删除,置顶,将头像图片圆形化处理。
<Android SwipeToDismiss:左右滑动删除ListView条目Item> Android的SwipeToDismiss是github上一个第三方开源框架(github ...
- Android笔记——导入Github开源项目CircleRefreshLayout
百度n久都找不到android studio导入第三方类库的正确方法,纠结睡不着 ,最后终于蒙到了方法,原来想太多了 ---------------------------------------- ...
- Android开发UI之开源项目第一篇——个性化控件(View)篇
原文:http://blog.csdn.net/java886o/article/details/24355907 本文为那些不错的Android开源项目第一篇——个性化控件(View)篇,主要介绍A ...
- Android非常实用的开源项目框架
我将文章中所描述的项目都集成在一个apk中,可以直接运行查看效果,2.2以上的机器都可以运行.因为不让直接上传apk文件,我压缩成了zip包 1. Universal-Image-Loader 实现异 ...
- android最火的开源项目
原文地址:http://www.csdn.net/article/2013-05-21/2815370-Android-open-source-projects-finale 此前,CSDN移动频道推 ...
随机推荐
- MVC 学习
基础概念学习:http://www.cnblogs.com/meetyy/p/3451933.html 路由:http://www.cnblogs.com/meetyy/p/3453189.html ...
- 骁龙820和KryoCPU:异构计算与定制计算的作用 【转】
本文转载自:https://www.douban.com/group/topic/89037625/ Qualcomm骁龙820处理器专为提供创新用户体验的顶级移动终端而设计.为实现消费者所期望的创新 ...
- Shell变量知识进阶
一,Shell中特殊且重要的变量 $0结合dirname和basename分别取出脚本名称和脚本路径 [root@192-168-3-163 scripts]# cat test.sh #!/bin/ ...
- ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图
类似题解 There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u, ...
- 【Coursera】Technology :Fifth Week(2)
The Ethernet Story Bob Metcalfe Bob 参与了 Xerox 研究项目,着手解决建造一个处处连接个人计算机的架构.当时,他们刚刚完成了 Internet 的开端 -具有 ...
- POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)
http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...
- 数据库与hadoop与分布式文件系统的区别和联系
转载一篇关系数据库与Hadoop的关系的文章 1. 用向外扩展代替向上扩展 扩展商用关系型数据库的代价是非常昂贵的.它们的设计更容易向上扩展.要运行一个更大的数据库,就需要买一个更大的机器.事实上,往 ...
- 【Python】【数据类型】
[序列更新&散列&切片]"""from array import arrayimport reprlibarray1 = array('d',(1,2,3 ...
- webdriver 的三种等待方式
1.显式等待 一个显式等待是你定义的一段代码,用于等待某个条件发生然后再继续执行后续代码. from selenium import webdriverfrom selenium.webdriver ...
- [ios]关于ios开发图片尺寸的建议
1.以后的应用程序,都使用AutoLayout, 不要再用绝对定位. 2.使用类似网页的方式来设计界面. 3.设计师好,程序员也好,尽量使用点这个单位进行思考,而不要使用像素.比如,你需要做44 x ...