EventBus只能单向传递,但是该传递不会丢失信息。
导入EventBus工程,将其创建成Library,如图:
前台代码
package com.pangbao.myeventbus;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
TextView tv = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv_id);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(this);
EventBus.getDefault().register(this);
}
@Override
public void onClick(View v) {
// 点击启动后台服务
Intent service = new Intent(this, PangService.class);
startService(service);
}
// 这里,EventBus回调接受消息,然后更新Main UI的TextView
public void onEventMainThread(Comment event) {
Log.d(this.getClass().getName(),
"onEventMainThread:" + event.toString());
tv.setText(event.str);
}
// EventBus回调接受消息
public void onEventBackgroundThread(Comment event) {
Log.d(this.getClass().getName(),
"onEventBackgroundThread:" + event.toString());
}
// EventBus回调接受消息
public void onEventAsync(Comment event) {
Log.d(this.getClass().getName(), "onEventAsync:" + event.toString());
}
}
后台代码package com.pangbao.myeventbus;
import de.greenrobot.event.EventBus;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class PangService extends Service {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
public void startEvent() {
new Thread(new Runnable() {
@Override
public void run() {
int id = 0;
for (int i = 0; i < 100; i++) {
Comment event = new Comment();
event.str = "hollow," + id + "word";
EventBus.getDefault().post(event);
id++;
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
startEvent();
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
接收数据类
package com.pangbao.myeventbus;
public class Comment {
public int id;
public String str;
@Override
public String toString() {
return "Comment [str=" + str + "]";
}
}
布局代码
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.pangbao.myeventbus.MainActivity" >
<TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_id"
android:layout_marginTop="114dp"
android:layout_toRightOf="@+id/tv_id"
android:text="接收" />
</RelativeLayout>
记得最后在AndroidManif.xml中注册
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pangbao.myeventbus"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".PangService"/>
</application>
</manifest>