您的位置:首页 > 其它

Eventbus简单使用

2016-08-16 15:10 323 查看
package com.bruce.eventbusdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity {

private TextView mMessageView;
private EditText mMessageET;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//注册事件
EventBus.getDefault().register(this);
mMessageView = (TextView) findViewById(R.id.tv);
mMessageET = (EditText) findViewById(R.id.edit);
findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = mMessageET.getText().toString();
if (TextUtils.isEmpty(message)) {
message = "defaule message";
}
EventBus.getDefault().post(new MessageEvent(message));
}
});
}

/**
* POSTING,-Subscriber will be called in the same thread, which is posting the event.
* <p/>
* MAIN,-Subscriber will be called in Android's main thread (sometimes referred to as UI thread).
* <p/>
* BACKGROUND,-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.
* <p/>
* 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.
*
* @param messageEvent
*/
@Subscribe(threadMode = ThreadMode.MAIN)
public void onShowMessageEvent(MessageEvent messageEvent) {
mMessageView.setText("Message" + messageEvent.getObject().toString());
/**
* 处理事件
*/
startActivity(new Intent(MainActivity.this, BActivity.class));
}

/**
* PostThread:
* 如果使用事件处理函数指定了线程模型为PostThread,那么该事件在哪个线程发布出来的,事件处理函数就会在这个线程中运行,也就是说发布事件和接收事件在同一个线程。
* 在线程模型为PostThread的事件处理函数中尽量避免执行耗时操作,因为它会阻塞事件的传递,甚至有可能会引起ANR。
* MainThread:
* 如果使用事件处理函数指定了线程模型为MainThread,那么不论事件是在哪个线程中发布出来的,该事件处理函数都会在UI线程中执行。
* 该方法可以用来更新UI,但是不能处理耗时操作。
* BackgroundThread:
* 如果使用事件处理函数指定了线程模型为BackgroundThread,那么如果事件是在UI线程中发布出来的,那么该事件处理函数就会在新的线程中运行,
* 如果事件本来就是子线程中发布出来的,那么该事件处理函数直接在发布事件的线程中执行。在此事件处理函数中禁止进行UI更新操作。
* Async:
* 如果使用事件处理函数指定了线程模型为Async,那么无论事件在哪个线程发布,该事件处理函数都会在新建的子线程中执行。
* 同样,此事件处理函数中禁止进行UI更新操作。
*/
@Override
protected void onDestroy() {
super.onDestroy();
// Unregister
EventBus.getDefault().unregister(this);
}
}
</pre><pre code_snippet_id="1833257" snippet_file_name="blog_20160816_3_3255557" name="code" class="java">
<pre name="code" class="java">package com.bruce.eventbusdemo;

/**
* Created by Administrator on 2016/8/16 0016.
*/
public class MessageEvent {
private Object object;

public MessageEvent(Object object) {
this.object = object;
}

public Object getObject() {
return object;
}

public void setObject(Object object) {
this.object = object;
}
}




<?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.bruce.eventbusdemo.MainActivity">

<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: