Android学习之EventBus基础篇
来源:互联网 发布:西师大知行学院咋样 编辑:程序博客网 时间:2024/06/05 02:17
一、概述
EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
Github地址:https://github.com/greenrobot/EventBus
二、实例演示
点击MainActivity页面里的跳转按钮进入SecondActivity页面,点击发送EventBus事件按钮,进行事件消息发送操作,我们在返回到MainActivity页面发现,我们成功的接收到SecondActivity页面发送过来的消息。
1.添加依赖:
compile 'org.greenrobot:eventbus:3.0.0'
2.MainActivity.java
package com.czhappy.eventbusdemo.activity;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.TextView;import com.czhappy.eventbusdemo.R;import com.czhappy.eventbusdemo.model.MessageEvent;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class MainActivity extends AppCompatActivity { private TextView receive_tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册eventBus EventBus.getDefault().register(this); receive_tv = (TextView) this.findViewById(R.id.receive_tv); } public void goSecondBtn(View view){ SecondActivity.startActivity(this); } @Override protected void onDestroy() { super.onDestroy(); //取消注册 EventBus.getDefault().unregister(this); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEventMainThread(MessageEvent event) { if (!TextUtils.isEmpty(event.getMsg())) { receive_tv.setText("接收到消息:" + event.getMsg()); } }}
3.activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="跳转" android:onClick="goSecondBtn"/> <TextView android:id="@+id/receive_tv" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:textColor="#333333" android:textSize="16sp"/></LinearLayout>
4.SecondActivity.java
package com.czhappy.eventbusdemo.activity;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import com.czhappy.eventbusdemo.R;import com.czhappy.eventbusdemo.model.MessageEvent;import org.greenrobot.eventbus.EventBus;/** * author: chenzheng * created on: 2017/8/2 8:08 * description: */public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } public void sendEventBtn(View view){ EventBus.getDefault().post(new MessageEvent("大家好,我是小明!")); } public static void startActivity(Context context){ Intent intent = new Intent(context, SecondActivity.class); context.startActivity(intent); }}
5.activity_second.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:orientation="vertical"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:text="发送EventBus事件" android:onClick="sendEventBtn"/></LinearLayout>
阅读全文
0 0
- Android学习之EventBus基础篇
- android 学习之 EventBus 源码初析
- EventBus使用之基础
- EventBus使用之基础
- EventBus使用之基础
- android EventBus基础介绍
- Android EventBus学习总结
- Android EventBus学习
- Android EventBus学习
- Android EventBus学习笔记
- Android开发系列通信篇之EventBus
- Android开发系列通信篇之EventBus
- Android开发系列通信篇之EventBus
- Android中通信篇之EventBus
- Android之EventBus
- Android之EventBus
- Android之EventBus
- android 技术之EventBus
- python之socket编程举例
- 解决log4cxx输出中文路径乱码问题
- [ JS 进阶 ] Repaint 、Reflow 的基本认识和优化
- 10版outlook主账户删不掉
- 【数据处理·Python】时间数据处理之Arrow库(转)
- Android学习之EventBus基础篇
- about 'Syntax error on token "Invalid Character", delete this token'
- C# 实现托盘,并显示气泡消息
- Android读取pdf文件及显示
- 新手入门,三言两语说编译:C和C++…
- JSP与Servlet的关系
- 三言两语说拷贝:深拷贝和浅拷贝
- 三言两语说new和malloc:new m…
- 三言两语说C++三性