案例效果:
当击btn_try按钮的时候,跳到第二个Activity,当点击第二个activity上面的First Event按钮的时候向第一个Activity发送消息,当第一个Activity收到消息后,一方面将消息Toast显示,一方面放入textView中显示。
activity_main.xml
<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"> <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>
<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.harvic.try_eventbus_1.SecondActivity" > <Button android:id="@+id/btn_first_event" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="First Event"/> </LinearLayout>
package com.harvic.other; /** * 用来构造消息的 * 对外提供一个方法getMsg,获取消息内容即可 * */ <span style="color:#FF0000;">public class FirstEvent { private String mMsg; public FirstEvent(String msg) { mMsg = msg; } public String getMsg(){ return mMsg; } }</span>
package com.example.tryeventbus_simple; import com.harvic.other.FirstEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { Button btn; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //注册EventBus EventBus.getDefault().register(this);</span> btn = (Button) findViewById(R.id.btn_try); tv = (TextView)findViewById(R.id.tv); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), SecondActivity.class); startActivity(intent); } }); } /** * 我们使用EventBus中最常用的onEventMainThread()函数来接收消息 */ 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(); } @Override protected void onDestroy(){ super.onDestroy(); EventBus.getDefault().unregister(this); } }
package com.example.tryeventbus_simple; import com.harvic.other.FirstEvent; import de.greenrobot.event.EventBus; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class SecondActivity extends Activity { private Button btn_FirstEvent; @Override protected 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) { //发送消息是使用EventBus中的Post方法来实现发送的,发送过去的是我们新建的类的实例 EventBus.getDefault().post( new FirstEvent("FirstEvent btn clicked")); } }); } }