EventBus 3.0使用
在没用eventBus之前一直用Android广播方式通知消息更新UI
广播写法
首先发送广播通知
Intent intent = new Intent();
intent.setAction("action.refreshFriend"); //名称自定义标识是哪个通知消息
sendBroadcast(intent);
接收广播通知
首先注册广播
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("action.refreshFriend");
registerReceiver(mRefreshBroadcastReceiver, intentFilter);
private BroadcastReceiver mRefreshBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals("action.refreshFriend"))
{
//更新UI
}
}
};
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mRefreshBroadcastReceiver); //销毁广播
}
----------------------------------------------------------------
https://github.com/greenrobot/EventBus
EventBus是Android的发布/订阅事件总线优化。
首先添加引用
compile 'org.greenrobot:eventbus:3.0.0'
MainActivity代码
package com.freexiaoyu.enevtbus; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode; import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick; public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv_test)
TextView tv_text;
@BindView(R.id.btn_post)
Button btn_post;
@BindView(R.id.btn_post2)
Button btn_post2;
@BindView(R.id.btn_post3)
Button btn_post3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
EventBus.getDefault().register(this);
} @Subscribe(threadMode = ThreadMode.MAIN)
public void helloEventBus(Event event) {
switch (event.getType()){
case 1:
tv_text.setText(event.getMessage().toString());
break;
case 2:
tv_text.setText(event.getMessage().toString());
break;
case 3:
tv_text.setText(event.getMessage().toString());
break;
} } @Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
} @OnClick({R.id.btn_post,R.id.btn_post2,R.id.btn_post3})
public void submit(View view) {
switch (view.getId()){
case R.id.btn_post:
EventBus.getDefault().post(new Event(1,"我是老大"));
break;
case R.id.btn_post2:
EventBus.getDefault().post(new Event(2,"我是老二"));
break;
case R.id.btn_post3:
Intent intent=new Intent(MainActivity.this,TestActivity.class);
startActivity(intent);
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical"> <TextView
android:id="@id/tv_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" /> <Button
android:id="@id/btn_post"
android:layout_width="match_parent"
android:layout_height="52.0dp"
android:text="EventBus" />
<Button android:id="@id/btn_post2"
android:layout_width="match_parent"
android:layout_height="52.0dp"
android:text="EventBus" />
<Button android:id="@id/btn_post3"
android:layout_width="match_parent"
android:layout_height="52.0dp"
android:text="跳转界面" />
</LinearLayout>
TestActivity代码
package com.freexiaoyu.enevtbus; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; import org.greenrobot.eventbus.EventBus; import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick; public class TestActivity extends AppCompatActivity {
@BindView(R.id.btn_post)
Button btn_post; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ButterKnife.bind(this);
} @OnClick({R.id.btn_post})
public void submit(View view) {
switch (view.getId()){
case R.id.btn_post:
EventBus.getDefault().post(new Event(3,"我是小三哈哈!"));
break;
}
} @Override
protected void onDestroy() {
super.onDestroy();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:id="@id/btn_post"
android:layout_width="match_parent"
android:layout_height="52.0dp"
android:text="我要更新上一页内容" />
</RelativeLayout>
Event代码
public class Event {
private int type;
private Object message;
public Event(int type, Object message){
this.type=type;
this.message=message;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public Object getMessage() {
return message;
}
public void setMessage(Object message) {
this.message = message;
}
}
DEMO地址 https://yunpan.cn/cB3SH2Ig7dAZU 访问密码 aad8
EventBus 3.0使用的更多相关文章
- 【热门技术】EventBus 3.0,让事件订阅更简单,从此告别组件消息传递烦恼~
一.写在前面 还在为时间接收而烦恼吗?还在为各种组件间的消息传递烦恼吗?EventBus 3.0,专注于android的发布.订阅事件总线,让各组件间的消息传递更简单!完美替代Intent,Handl ...
- Android消息传递之EventBus 3.0使用详解
前言: 前面两篇不仅学习了子线程与UI主线程之间的通信方式,也学习了如何实现组件之间通信,基于前面的知识我们今天来分析一下EventBus是如何管理事件总线的,EventBus到底是不是最佳方案?学习 ...
- Android EventBus 3.0.0 使用总结
转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/6039221.html 本文出自[赵彦军的博客] 前言 EventBus框架 EventBus是一个通用的叫法 ...
- Android EventBus 3.0 实例使用详解
EventBus的使用和原理在网上有很多的博客了,其中泓洋大哥和启舰写的非常非常棒,我也是跟着他们的博客学会的EventBus,因为是第一次接触并使用EventBus,所以我写的更多是如何使用,源码解 ...
- EventBus 3.0使用详解
01 前言 当我们进行项目开发的时候,往往是需要应用程序的各组件.组件与后台线程间进行通信,比如在子线程中进行请求数据,当数据请求完毕后通过Handler或者是广播通知UI,而两个Fragment之家 ...
- EventBus 3.0使用相关
一 引入方法 可以去github的官网中下载EventBus的相关资源 地址:https://github.com/greenrobot/EventBus 当然还有他的官方网站 http://gre ...
- EventBus 3.0源码解析
现在网上讲解EventBus的文章大多数都是针对2.x版本的,比较老旧,本篇文章希望可以给大家在新版本上面带来帮助. EventBus 是专门为Android设计的用于订阅,发布总线的库,用到这个库的 ...
- 【转】EventBus 3.0使用详解
原文:https://www.jianshu.com/p/f9ae5691e1bb 01 前言 当我们进行项目开发的时候,往往是需要应用程序的各组件.组件与后台线程间进行通信,比如在子线程中进行请求数 ...
- Android 框架学习2:源码分析 EventBus 3.0 如何实现事件总线
Go beyond yourself rather than beyond others. 上篇文章 深入理解 EventBus 3.0 之使用篇 我们了解了 EventBus 的特性以及如何使用,这 ...
随机推荐
- Python缩进
今天练习代码的时候发现一个问题,练习类,我在notepad++上写的代码运行后,复制到pycharm上运行然后报错,看代码 #---coding:utf-8--- #定义一个Person类然后实例化 ...
- 【筛法求素数】【质因数分解】bzoj2721 [Violet 5]樱花
http://www.cnblogs.com/rausen/p/4138233.html #include<cstdio> #include<iostream> using n ...
- replicate-rewrite-db
replicate-rewrite-db: Tells the slave to translate the default database (that is, the one selected b ...
- Windows操作系统消费者价值亮点
在讨论Windows操作系统之前,我们先看看消费者是什么. 消费者是产品和服务的最终使用者 ,其购买商品的目的主要是用于个人或家庭需要. 那么消费者的需求是什么,是使用,所以谁能给消费者更好的使用体验 ...
- iOS 汉字转拼音
- (NSString *)getFirstString:(ICCustom *)custom { NSMutableString *source = [custom.merchantAbbr ...
- 数据库知识整理<五>
简单的数据查询: 5.1查询的基本结构: Sql语句:select [distinct] (* | column [alias],...) from table [where condition] [ ...
- 关于X锁的问题--由select+X锁是否持有到事务结束的误区
前言:看了宋桑的文章<一次意外的X锁不阻塞问题>,结合本人的测试,说明一下我对select中使用X锁是否会持有到事务结束产生的误区: 详情不多说了,详见宋桑的<一次意外的X锁不阻塞问 ...
- 手机数据抓包以及wireshark技巧
本文主要讨论一种非常方便的抓取Android和iphone手机网络数据包的办法,以及介绍wireshark最常用的技巧 抓包工具介绍 (1).网页抓包工具 Chrome浏览器插件 FireBug 插件 ...
- 随机数是骗人的,.Net、Java、C为我作证
几乎所有编程语言中都提供了"生成一个随机数"的方法,也就是调用这个方法会生成一个数,我们事先也不知道它生成什么数.比如在.Net中编写下面的代码: Random rand = ne ...
- 使用 sqlcmd 运行 Transact-SQL 脚本文件
在数据恢复时遇到问题:bat文件批处理的指导文档 https://msdn.microsoft.com/zh-cn/library/ms170572%28v=sql.120%29.aspx