网友通过本文主要向大家介绍了eventbus3.0使用,eventbus使用,android eventbus使用,eventbus怎么使用,eventbus如何使用等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
EventBus的使用,EventBus使用
前言:第一篇文章,EventBus很多项目都在用,本文参照http://blog.csdn.net/harvic880925/article/details/40660137,自己做了一遍。把有用的挑拣出来,记录一下啊
一、概述
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) {} public void onEventMainThread(AnyEventType event) {} public void onEventBackgroundThread(AnyEventType event) {} public void onEventAsync(AnyEventType event) {}
onEvent:如果使用onEvent作为订阅函数,那么该事件在哪个线程发布出来的,onEvent就会在这个线程中运行,也就是说发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
onEventMainThread:如果使用onEventMainThread作为订阅函数,那么不论事件是在哪个线程中发布出来的,onEventMainThread都会在UI线程中执行,接收事件就会在UI线程中运行,这个在Android中是非常有用的,因为在Android中只能在UI线程中跟新UI,所以在onEvnetMainThread方法中是不能执行耗时操作的。
onEventBackground:如果使用onEventBackgrond作为订阅函数,那么如果事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
onEventAsync:使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
(5)解除注册
eventBus.unregister(this);
顺序就是这么个顺序,可真正让自己写,估计还是云里雾里的,下面举个例子来说明下。
首先,在EventBus中,获取实例的方法一般是采用EventBus.getInstance()来获取默认的EventBus实例,当然你也可以new一个又一个,个人感觉还是用默认的比较好,以防出错。
二、实战
先给大家看个例子:
当击跳转到第二个界面按钮的时候,跳到第二个Activity,当点击第二个activity上面的t按钮的时候向第一个Activity发送消息,第一个按钮使用的是
新建一个Activity,SecondActivity布局(activity_two.xml)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/bt_text_post" android:id="@+id/bt_post"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/bt_text_post2" android:layout_below="@id/bt_post" android:id="@+id/bt_post2"/> </RelativeLayout>
MainActivity.java
package com.nova.eventbus_1; 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 org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; public class MainActivity extends AppCompatActivity { private Button button_jump; private TextView tv_notify; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_jump= (Button) findViewById(R.id.bt_jump); tv_notify= (TextView) findViewById(R.id.tv_notify); button_jump.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { <strong> Intent intent=new Intent(MainActivity.this,SecondActivity.class); startActivityForResult(intent,1);</strong> } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case 1: if(resultCode==1){ String returnedData=data.getStringExtra("com.nova.eventbus_1.data_return"); tv_notify.setText(returnedData); Toast.makeText(MainActivity.this,returnedData,Toast.LENGTH_LONG).show(); Log.i("现在的线程",Thread.currentThread().getName()); } } } }
SecondActivity.java
package com.nova.eventbus_1; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import org.greenrobot.eventbus.EventBus; /** * Created by Nova on 2016/4/11. */ public class SecondActivity extends AppCompatActivity { private Button button_post; private