Android EventBus發布/訂閱事件匯流排

來源:互聯網
上載者:User

標籤:android   eventbus   事件   

做過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{@Overrideprotected 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);}@Overridepublic 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());}}@Overridepublic 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{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_two);Button button = (Button)findViewById(R.id.button1);button.setOnClickListener(this);}@Overridepublic 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
如有轉載未註明出處部落格網址,或者沒有得到作者的同意。作者持有著作權權。


Android EventBus發布/訂閱事件匯流排

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.