做过Android开发都会陆续用到这个开源库EventBus。EventBus是一款针对Android优化的发布/订阅事件总线。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,线程之间传递消息.优点是开销小,代码更优雅。以及将发送者和接收者解耦。下载EventBus的类库源码:https://github.com/greenrobot/EventBus。下面说说简单用法。
本文项目资源下载:
一、先定义一个消息实体类MainSendEvent
package com.example.eventbusdemo;
/**
* 事件消息实体类
* @author mmxs
*
*/
public class MainSendEvent {
protected String mstrMsg;
public MainSendEvent(String msg) {
mstrMsg = msg;
}
public String getStringMsgData(){
return mstrMsg;
}
}
二、MainActivity
package com.example.eventbusdemo;
import de.greenrobot.event.EventBus;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//事件注册
EventBus.getDefault().register(this);
InitUI();
}
private void InitUI() {
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Intent intent = new Intent();
intent.setClass(MainActivity.this, TwoActivity.class);
startActivity(intent);
break;
default:
break;
}
}
//事件接受
public void onEventMainThread(MainSendEvent event){
if(event != null){
Toast.makeText(getApplicationContext(),
"MainActivity接受数据" + event.getStringMsgData(),
Toast.LENGTH_LONG).show();
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(event.getStringMsgData());
}
}
@Override
public void onDestroy() {
//取消注册
EventBus.getDefault().unregister(this);
super.onDestroy();
}
}
三、TwoActivity
package com.example.eventbusdemo;
import de.greenrobot.event.EventBus;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 第二个TwoActivity发送事件
* @author mmsx
*
*/
public class TwoActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
//事件发送
EventBus.getDefault().post(new MainSendEvent("from TwoActivity msg"));
break;
default:
break;
}
}
}
四、两个activity的布局
1、activity_main
<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/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二个activity" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
2、activity_two
<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/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MainActivity" />
</LinearLayout>
在这上面总体上实践就是在activity间消息传递。我们先进去的是MainActivity。然后按button进入第二个activity。在第二个activity按一下button发送数据到MainActivity,用Toat弹出以及settext。返回MainActivity后能看到settext效果。
五、效果图
六、简单来说的流程
1、事件注册或者订阅
EventBus.getDefault().register(this);
2、事件注册或者订阅后的接受
public void onEventMainThread(MainSendEvent event){
if(event != null){
Toast.makeText(getApplicationContext(),
"MainActivity接受数据" + event.getStringMsgData(),
Toast.LENGTH_LONG).show();
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText(event.getStringMsgData());
}
}
3、事件注册或者订阅取消
EventBus.getDefault().unregister(this);
这上面3点是一起,同一个页面。
4、事件的发送者
EventBus.getDefault().post(new MainSendEvent("from TwoActivity msg"));
七、参考文章
http://blog.csdn.net/harvic880925/article/details/40660137
http://blog.csdn.net/lmj623565791/article/details/40920453
最后
项目资源下载:
转载请注明出处的博客网址: http://blog.csdn.net/qq_16064871
如有转载未注明出处博客网址,或者没有得到作者的同意。作者持有版权所有权。