package com.example.eventbustest;
import de.greenrobot.event.EventBus;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private TextView textTv;
private Button secondBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("hq", "MainActivity onCreate Thread id-->" + Thread.currentThread().getId());
EventBus.getDefault().register(this);
textTv = (TextView) findViewById(R.id.text_tv);
secondBtn = (Button) findViewById(R.id.second_activity_btn);
secondBtn.setOnClickListener(this);
}
/**
* 接收事件就会在UI线程中运行,无耗时操作
* @param msg
*/
public void onEventMainThread(MyMsg msg) {
textTv.setText(msg.getMsg());
Toast.makeText(MainActivity.this, "onEventMainThread"+msg.getMsg(), Toast.LENGTH_SHORT)
.show();
}
public void onEventMainThread(String msg) {
textTv.setText("onEventMainThread:" + msg);
Log.v("hq", "onEventMainThread Thread id-->" + Thread.currentThread().getId());
}
/**
* 发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
* @param msg
*/
public void onEvent(String msg) {
textTv.setText("onEvent:" + msg + 1);
Log.v("hq", "onEvent Thread id-->" + Thread.currentThread().getId());
}
/*
* 使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
*/
public void onEventAsync(String msg) {
Log.v("hq", "onEventAync Thread id-->" + Thread.currentThread().getId());
}
/**
* 事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
* @param msg
*/
public void onEventBackgroundThread(String msg) {
Log.v("hq", "onEventBackgroundThread Thread id-->" + Thread.currentThread().getId());
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.second_activity_btn:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
package com.example.eventbustest;
import de.greenrobot.event.EventBus;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private TextView textTv;
private Button secondBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("hq", "MainActivity onCreate Thread id-->" + Thread.currentThread().getId());
EventBus.getDefault().register(this);
textTv = (TextView) findViewById(R.id.text_tv);
secondBtn = (Button) findViewById(R.id.second_activity_btn);
secondBtn.setOnClickListener(this);
}
/**
* 接收事件就会在UI线程中运行,无耗时操作
* @param msg
*/
public void onEventMainThread(MyMsg msg) {
textTv.setText(msg.getMsg());
Toast.makeText(MainActivity.this, "onEventMainThread"+msg.getMsg(), Toast.LENGTH_SHORT)
.show();
}
public void onEventMainThread(String msg) {
textTv.setText("onEventMainThread:" + msg);
Log.v("hq", "onEventMainThread Thread id-->" + Thread.currentThread().getId());
}
/**
* 发布事件和接收事件线程在同一个线程。使用这个方法时,在onEvent方法中不能执行耗时操作,如果执行耗时操作容易导致事件分发延迟。
* @param msg
*/
public void onEvent(String msg) {
textTv.setText("onEvent:" + msg + 1);
Log.v("hq", "onEvent Thread id-->" + Thread.currentThread().getId());
}
/*
* 使用这个函数作为订阅函数,那么无论事件在哪个线程发布,都会创建新的子线程在执行onEventAsync.
*/
public void onEventAsync(String msg) {
Log.v("hq", "onEventAync Thread id-->" + Thread.currentThread().getId());
}
/**
* 事件是在UI线程中发布出来的,那么onEventBackground就会在子线程中运行,如果事件本来就是子线程中发布出来的,那么onEventBackground函数直接在该子线程中执行。
* @param msg
*/
public void onEventBackgroundThread(String msg) {
Log.v("hq", "onEventBackgroundThread Thread id-->" + Thread.currentThread().getId());
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Override
protected void onStop() {
super.onStop();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.second_activity_btn:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
package com.example.eventbustest;
public class MyMsg {
private String msg;
public MyMsg(String msg) {
super();
this.msg = msg;
}
public String getMsg() {
return msg;
}
}