apply plugin: 'com.android.application'android { compileSdkVersion 26 buildToolsVersion "27.0.1" defaultConfig { applicationId "com.bwie.eventbuslian" minSdkVersion 16 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" javaCompileOptions { annotationProcessorOptions { arguments = [eventBusIndex: 'com.bwie.butterknife.MyEventBusIndex'] } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' compile 'org.greenrobot:eventbus:3.1.1' testCompile 'junit:junit:4.12' annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'}
package com.bwie.eventbuslian;/** * Created by lenovo on 2017/11/29. */public class First { private String Msg; public First(String msg) { Msg = msg; } public String getMsg() { return Msg; }}
package com.bwie.eventbuslian;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.TextView;import android.widget.Toast;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class Main2Activity extends AppCompatActivity { private TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); EventBus.getDefault().register(this); tv2= (TextView) findViewById(R.id.tv_2); findViewById(R.id.bt_2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(getApplicationContext(),SecondActivity.class)); } }); } @Subscribe(threadMode = ThreadMode.MAIN) public void onEventBusMainThread(First first){ String f="消息是"+first.getMsg(); tv2.setText(f); Toast.makeText(this,f, Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }}
package com.bwie.eventbuslian;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.TextView;import android.widget.Toast;import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;import org.greenrobot.eventbus.ThreadMode;public class MainActivity extends AppCompatActivity { private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册EventBus EventBus.getDefault().register(this); tv = (TextView) findViewById(R.id.tv); findViewById(R.id.bt).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(getApplicationContext(),SecondActivity.class)); } }); findViewById(R.id.bt_main2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { startActivity(new Intent(MainActivity.this,Main2Activity.class)); } }); } @Subscribe(threadMode = ThreadMode.MAIN)public void onEventMainThread(First first){ String msg="接受到的消息是"+first.getMsg(); Log.e("哈哈哈哈",msg); tv.setText(msg); Toast.makeText(this,"hahahahahahaahahaha"+msg, Toast.LENGTH_SHORT).show();} @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this);//取消注册 }}
package com.bwie.eventbuslian;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import org.greenrobot.eventbus.EventBus;public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Button bt= (Button) findViewById(R.id.bt_fa); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EventBus.getDefault().post(new First("郭宇雷...shit")); finish(); } }); }}