Android中的EventBus使用(一)
来源:互联网 发布:c语言 extern static 编辑:程序博客网 时间:2024/04/28 15:07
一、概述
EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。
1、下载EventBus的类库
源码:https://github.com/greenrobot/EventBus
2、基本使用
(1)自定义一个类,可以是空类,比如:
public class AnyEventType {
public AnyEventType(){}
}
(2)在要接收消息的页面注册:
- eventBus.register(this);
(3)发送消息
- eventBus.post(new AnyEventType event);
(4)接受消息的页面实现(共有四个函数,各功能不同,这是其中之一,可以选择性的实现,这里先实现一个):
- public void onEvent(AnyEventType event) {}
- eventBus.unregister(this);
首先,在EventBus中,获取实例的方法一般是采用EventBus.getInstance()来获取默认的EventBus实例,当然你也可以new一个又一个,个人感觉还是用默认的比较好,以防出错。
二、实践:
先给大家看个例子:
当击btn_try按钮的时候,跳到第二个Activity,当点击第二个activity上面的First Event按钮的时候向第一个Activity发送消息,当第一个Activity收到消息后,一方面将消息Toast显示,一方面放入textView中显示。
按照下面的步骤,下面来建这个工程:
导包:ottoandeventbusdemo/libs/eventbus.jar
1、基本框架搭建
想必大家从一个Activity跳转到第二个Activity的程序应该都会写,这里先稍稍把两个Activity跳转的代码建起来。后面再添加EventBus相关的玩意。
MainActivity布局(activity_main.xml)
<?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:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.ottoandeventbusdemo.MainActivity"> <Button android:id="@+id/btn_try" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="btn_bty" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="match_parent" /></LinearLayout>新建一个Activity,SecondActivity布局(activity_second.xml)
<?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" tools:context="com.example.administrator.ottoandeventbusdemo.SecondActivity" > <Button android:id="@+id/btn_first_event" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="First Event"/></LinearLayout>
MainActivity.java (点击btn跳转到第二个Activity)
2、新建一个类FirstEvent
package com.example.administrator.ottoandeventbusdemo;/** * Created by Administrator on 2017/1/17. */public class FirstEvent { private String mMsg; public FirstEvent(String msg) { // TODO Auto-generated constructor stub mMsg = msg; } public String getMsg(){ return mMsg; }}这个类很简单,构造时传进去一个字符串,然后可以通过getMsg()获取出来。
3、在要接收消息的页面注册EventBus:
在上面的GIF图片的演示中,大家也可以看到,我们是要在MainActivity中接收发过来的消息的,所以我们在MainActivity中注册消息。
通过我们会在OnCreate()函数中注册EventBus,在OnDestroy()函数中反注册。所以整体的注册与反注册的代码如下:
4、发送消息
发送消息是使用EventBus中的Post方法来实现发送的,发送过去的是我们新建的类的实例!- EventBus.getDefault().post(new FirstEvent("FirstEvent btn clicked"));
package com.example.administrator.ottoandeventbusdemo;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import de.greenrobot.event.EventBus;/** * Created by Administrator on 2017/1/17. */public class SecondActivity extends AppCompatActivity { private Button btn_FirstEvent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); btn_FirstEvent = (Button) findViewById(R.id.btn_first_event); btn_FirstEvent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub EventBus.getDefault().post( new FirstEvent("FirstEvent btn clicked")); } }); }}
5、接收消息
接收消息时,我们使用EventBus中最常用的onEventMainThread()函数来接收消息,具体为什么用这个,我们下篇再讲,这里先给大家一个初步认识,要先能把EventBus用起来先。在MainActivity中重写onEventMainThread(FirstEvent event),参数就是我们自己定义的类:
在收到Event实例后,我们将其中携带的消息取出,一方面Toast出去,一方面传到TextView中;
- public void onEventMainThread(FirstEvent event) {
- String msg = "onEventMainThread收到了消息:" + event.getMsg();
- Log.d("harvic", msg);
- tv.setText(msg);
- Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
- }
package com.example.administrator.ottoandeventbusdemo;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import de.greenrobot.event.EventBus;public class MainActivity extends AppCompatActivity { private Button btn; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册EventBus EventBus.getDefault().register(this); btn = (Button) findViewById(R.id.btn_try); tv = (TextView) findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } public void onEventMainThread(FirstEvent event) { String msg = "onEventMainThread收到了消息:" + event.getMsg(); Log.d("eventAndotto", msg); tv.setText(msg); Toast.makeText(this, msg, Toast.LENGTH_LONG).show(); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }}好了,到这,基本上算初步把EventBus用起来了,下篇再讲讲EventBus的几个函数,及各个函数间是如何识别当前如何调用哪个函数的。
- Android中的EventBus使用(一)
- Android中EventBus使用详解(一)
- EventBus在Android中的简单使用
- EventBus使用(一)
- EventBus使用(一)
- EventBus使用详解(一)
- EventBus使用(一)
- EventBus的使用(一)
- 初步使用EventBus (一)
- Eventbus的使用(一)
- EventBus使用记录(一)
- android中的EventBus
- Android中的 EventBus
- Android EventBus框架(一)之使用详细介绍
- Android EventBus 3.0 使用及源码解析一
- Android Studio中 EventBus的使用demo(一)
- Android EventBus框架(一)之使用详细介绍
- Android - EventBus使用 备忘
- 洛谷 P1019 单词接龙
- Spring -- 依赖注入源码分析
- Tricks in decide the index in the binary search with duplicate elements
- pva-faster-rcnn配置过程出现的问题(个人笔记)
- win10使用远程桌面时提示“你的凭据不工作”报错的解决办法
- Android中的EventBus使用(一)
- 3.4 迭代器
- 分布式事务一致性解决方案
- Redis安装和自启动配置
- java 利用httpclient 3.1 和 httpclient4.1.2发送post请求
- 洛谷 P1603 斯诺登的密码
- 米洛个人修炼术:如何倍增你的功力修行速度达成健康体魄
- Mysql 增量备份和全量备份
- 如何用JavaScript获取<div>*</div>标签中内容& 内容添加