編輯:關於 Android 程式設計
EventBus 是一套針對 Android 最佳化過的發布/訂閱事件總線,主要用來取代 Intent、Handler 與 Broadcast,在 Fragment、Activity、Service 與各執行緒之間傳遞訊息。它的優點是開銷小、程式碼乾淨,並能把訊息的發送者與接收者徹底解耦。
EventBus 類庫原始碼:EventBus_jb51.rar
1. 定義一個事件類別,內容可以是空的:
public class AnyEventType {
public AnyEventType() {}
}
2. 在接收方註冊:
eventBus.register(this);
3. 發送訊息:
eventBus.post(new AnyEventType());
4. 在接收方實作對應的接收方法(EventBus 提供四種方法,可依需求選擇,這裡先實作一種):
public void onEvent(AnyEventType event) { }
5. 註銷:
eventBus.unregister(this);
順序大致如此,但真正動手時仍容易糊塗,因此下面用一個完整例子說明。取得實例時,建議一律使用 EventBus.getDefault(),避免自行 new 出多個物件造成訊息收不到。
目標:點擊 btn_try 跳到第二個 Activity;在第二頁點「First Event」按鈕後向第一頁發送訊息;第一頁收到後,一方面用 Toast 提示,一方面把內容顯示在 TextView 上。

先把兩個頁面的骨架搭好。activity_main.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://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>
activity_second.xml:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://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>
MainActivity 先只負責跳轉:
public class MainActivity extends Activity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn_try);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
});
}
}
接著建立事件類別 FirstEvent,構造時帶入一則訊息,並提供 getter:
package com.harvic.other;
public class FirstEvent {
private String mMsg;
public FirstEvent(String msg) {
mMsg = msg;
}
public String getMsg() {
return mMsg;
}
}
由於要接收訊息的是 MainActivity,註冊就寫在它的 onCreate 中,並在 onDestroy 註銷:
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.getDefault().register(this);
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);
}
});
}
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);
}
}
發送端用 post(),傳過去的就是事件類別的實例:
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.getDefault().post(new FirstEvent("FirstEvent btn clicked"));
}
});
}
}
接收端這裡選用 onEventMainThread(),因為它保證回呼發生在主執行緒,可以直接更新 UI;至於其他三種接收方法與 EventBus 的內部識別機制,留待下一篇再深入說明。
原始碼:EventBus_jb51.rar
微信朋友圈怎麼只發純文字?長按相機鍵就能做到
微信除了聊天,朋友圈也是很多人每天記錄生活的地方。不過有時候只想寫兩句心情,不想配圖,卻發現按下相機一定要選照片——其實這是介面的小陷阱,只要多按一會兒就好。 只發文字、
自定義 RadioButton:讓文字四邊的圖片尺寸可以隨意設定
好久沒更新,寫這篇技術文的時候感覺很多東西都生疏了。於是心有感慨:我們做技術的,長時間不搞技術,那就是被技術搞。所以攻守之間,大家謹慎思量。 冬天已過,放假出去走動
在 Android 中使用 Java 8 Lambda 表達式:Retrolambda 實戰配置
寫 Java 的開發者大概都遇過這種苦惱:想傳一段行為進去,卻得先 new 一個匿名內部類,動輒五六行只為了呼叫一個方法。Java 8 引進的 Lambda 表達式正是為
Servlet 讀書筆記:生命週期、Session、轉發重定向與線程安全
環境準備 想編譯 Servlet,先把 Tomcat 的 common/lib 底下的 jsp_api.jar 與 servlet_api.jar 複製到 JDK 的 l