EventBus
========
EventBus is an Android optimized publish/subscribe event bus. A typical use case for Android apps is gluing Activities, Fragments, and background threads together. Conventional wiring of those elements often introduces ${IJ_BASE_PACKAGE}plex and error-prone dependencies and life cycle issues. With EventBus propagating listeners through all participants (e.g. background service -> activity -> multiple fragments or helper classes) be${IJ_BASE_PACKAGE}es deprecated. EventBus decouples event senders and receivers and thus simplifies ${IJ_BASE_PACKAGE}munication between app ${IJ_BASE_PACKAGE}ponents. Less code, better quality. And you don't need to implement a single interface!
General Usage and API
---------------------
In EventBus, subscribers implement event handling methods and register themselves to the bus. Posted events are delivered to matching event handling methods based on their event type (the Java class/interfaces implemented by the event).
Using EventBus takes four simple steps:
1. Implement any number of event handling methods in the subscriber:<br/>
<code>public void onEvent(AnyEventType event) {}</code>
2. Register subscribers:<br/>
<code>eventBus.register(this);</code>
3. Post events to the bus:<br/>
<code>eventBus.post(event);</code>
4. Unregister subscriber:<br/>
<code>eventBus.unregister(this);</code>
Add EventBus to your project
----------------------------
Starting with version 2.0.1, EventBus is pushed to [Maven Central](http://search.maven.org/\#search%7Cga%7C1%7Cg%3A%22de.greenrobot%22%20AND%20a%3A%22eventbus%22) repository, so you can simply add a dependency (group ID "de.greenrobot" and arifact ID "eventbus"). If you do not use Maven or Gradle, download the jar from [Maven Central](http://search.maven.org/\#search%7Cga%7C1%7Cg%3A%22de.greenrobot%22%20AND%20a%3A%22eventbus%22) and copy it into the libs folder of your Android project.
Delivery Threads
----------------
EventBus can deliver events in other threads independently from the posting thread. Threading is crucial to all Android apps, and EventBus will make threading easier. In Android development, UI changes must be done in the UI thread, while networking is forbidden here. If you want to do both networking and UI using standard Android API, you will need to take care of thread transistions, e.g. by using AsyncTask. If you use an event-based approach using EventBus, this gets simpler.
In EventBus, each event handling method is associated with a thread mode (have a look at the ThreadMode enum). The thread mode defines in which thread the event handling mehtod is called:
* **PostThread:** Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery implies the least overhead because it avoids thread switching ${IJ_BASE_PACKAGE}pletely. Thus this is the re${IJ_BASE_PACKAGE}mended mode for simple tasks that are known to ${IJ_BASE_PACKAGE}plete is a very short time without requiring the main thread. Event handlers using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.
* **MainThread:** Subscriber will be called in Android's main thread (sometimes referred to as UI thread). If the posting thread is the main thread, event handler methods will be called directly. Event handlers using this mode must return quickly to avoid blocking the main thread.
* **BackgroundThread:** Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single background thread that will deliver all its events sequentially. Event handlers using this mode should try to return quickly to avoid blocking the background thread.
* **Async:** Event handler methods are called in a separate thread. This is always independent from the posting thread and the main thread. Posting events never wait for event handler methods using this mode. Event handler methods should use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus uses a thread pool to efficiently reuse threads from ${IJ_BASE_PACKAGE}pleted asynchronous event handler notifications.
*Example:* Consider your subscriber updates the UI, but the triggering event is posted by a background thread (using eventBus.post(event)). The name of the event handling method should be onEventMainThread. EventBus takes care of calling the method in the main thread without any further code required
Sticky Events
-------------
Some events carry information that is of interest after the event is posted. For example, this could be an event signalizing that some initialization is ${IJ_BASE_PACKAGE}plete. Or if you have some sensor or location data and you want to hold on the most recent values. Instead of implementing your own caching, you can use sticky events. EventBus keeps the last sticky event of a certain type in memory. The sticky event can be delivered to subscribers or queried explicitly. Thus, you don't need any special logic to consider already available data.
API-wise events are made sticky by using postSticky(Object event) instead of post(Object event). Subscribers that want to get previously posted sticky events, use registerSticky(...) instead of register(...). Alternatively, the last sticky event of a certain event type can be queried by using getStickyEvent(Class<?> eventType).
Additional Features and Notes
-----------------------------
* **NOT based on annotations:** Querying annotations are slow on Android, especially before Android 4.0. Have a look at this [Android bug report](http://code.google.${IJ_BASE_PACKAGE}/p/android/issues/detail?id=7811)
* **Based on conventions:** Event handling methods are called "onEvent" (you could supply different names, but this is not encouraged).
* **Performanced optimized:** It's probably the fastest event bus for Android.
* **Tiny:** The jar is less than 30 KBytes.
* **Convenience singleton:** You can get a process wide event bus instance by calling EventBus.getDefault(). You can still call new EventBus() to create any number of local busses.
* **Subscriber and event inheritance:** Event handler methods may be defined in super classes, and events are posted to handlers of the event's super classes including any implemented interfaces. For example, subscriber may register to events of the type Object to receive all events posted on the event bus.
* **Selective registration:** It's possible to register only for specific event types. This also allows subscribers to register only some of their event handling methods for main thread event delivery.
Comparison with Square's Otto
-----------------------------
Otto is another event bus library for Android; actually it's a fork of Guava's EventBus. greenrobot's EventBus and Otto share some basic semantics (register, post, unregister, ...), but there are differences which the following table summarizes:
<table>
<tr>
<th></th>
<th>EventBus</th>
<th>Otto</th>
</tr>
<tr>
<th>Declare event handling methods</th>
<td>Name conventions</td>
<td>Annotations</td>
</tr>
<tr>
<th>Event inheritance</th>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<th>Subscriber inheritance</th>
<td>Yes</td>
<td>No</td>
</tr>
<tr>
<th>Cache most recent events</th>
<td>Yes, sticky events</td>
<td>No</td>
</tr>
<tr>
<th>Event producers (e.g. for coding cached events)</th>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<th>Event delivery in posting thread</th>
<td>Yes (Default)</td>
<td>Yes</td>
</tr>
<tr>
<th>Event delivery in main thread</th>
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ECSHOP手机版源码 (2000个子文件)
unzip.c 50KB
zip.c 38KB
mztools.c 8KB
ioapi.c 4KB
bootstrap.css 82KB
bootstrap-responsive.css 11KB
main.css 7KB
default.css 4KB
stylesheet.css 1KB
wizard-568h.css 833B
index.css 827B
wizard.css 740B
safestack.h 180KB
obj_mac.h 133KB
ssl.h 101KB
evp.h 51KB
asn1.h 51KB
ec.h 45KB
ASIHTTPRequest.h 44KB
x509.h 44KB
engine.h 40KB
objects.h 36KB
bn.h 35KB
tls1.h 34KB
bio.h 32KB
x509v3.h 32KB
ts.h 30KB
asn1t.h 29KB
ssl3.h 26KB
symhacks.h 23KB
ocsp.h 23KB
crypto.h 23KB
rsa.h 22KB
x509_vfy.h 22KB
pem.h 21KB
cms.h 19KB
asn1_mac.h 19KB
des_old.h 18KB
dso.h 18KB
pkcs7.h 18KB
ui.h 16KB
ecmobile.h 16KB
err.h 15KB
MobClick.h 14KB
unzip.h 13KB
pkcs12.h 12KB
dsa.h 12KB
JSONKit.h 11KB
WXApiObject.h 11KB
des.h 11KB
ssl2.h 11KB
ecdsa.h 10KB
e_os2.h 10KB
dh.h 10KB
conf.h 10KB
zip.h 9KB
lhash.h 9KB
Bee_Message.h 8KB
Bee_UIScrollView.h 8KB
Bee_Database.h 8KB
dtls1.h 8KB
krb5_asn.h 7KB
sha.h 7KB
Reachability.h 7KB
BeeUIStyle+Property.h 7KB
ossl_typ.h 7KB
Bee_HTTPRequest.h 7KB
Bee_View.h 7KB
Bee_Precompile.h 7KB
CSSTokenizer.h 6KB
srtp.h 6KB
opensslconf.h 6KB
Bee_HTTPProtocol2.h 6KB
Bee_Socket.h 6KB
Bee_UIStyleParser.h 6KB
kssl.h 6KB
seed.h 6KB
IFlySpeechRecognizer.h 6KB
modes.h 5KB
Bee_Log.h 5KB
rand.h 5KB
aes.h 5KB
NSObject+BeeProperty.h 5KB
Bee_UIBoard.h 5KB
ASICacheDelegate.h 5KB
srp.h 5KB
Bee_UIMetrics.h 5KB
blowfish.h 5KB
Bee_UISignal.h 5KB
camellia.h 5KB
crypt.h 5KB
Bee_UITipsView.h 5KB
NSString+BeeExtension.h 5KB
md4.h 5KB
md5.h 5KB
ecdh.h 5KB
buffer.h 5KB
ASINetworkQueue.h 5KB
IFlySpeechUnderstander.h 4KB
Bee_ActiveObject.h 4KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- ...+...2018-09-10能不能给一份说明书或者是技术文本呀!就是一个调试界面什么都没有了!里面的东西不知道能不能用 !
- Echo-Ran2018-10-20基本可以用
PPP2015
- 粉丝: 1
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
- SpringBoot整合mongodb学习MongoTemplate和MongoRepository两种方式CRUD使用.zip
- SpringBoot整合MongoDB实现对数据库的CRUD小demo.zip
- Python操作MongoDB数据库的基本一些操作 .zip
- NOSQL数据库监控工具,目前实现了对Redis、MongoDB的监控功能 .zip
- mongoDB数据库的增删改查,以及所需要的配置.zip
- mongodb数据库idea测试.zip
- koa 分别 连接 mysql、mongodb数据库操作.zip
- 基于pytorch实现的人体部件分割源码+模型.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功