Android 寮€婧愭鏋?( 涓?) 浜嬩欢鎬荤嚎---EventBus

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 寮€婧愭鏋?( 涓?) 浜嬩欢鎬荤嚎---EventBus相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%96%87%e7%ab%a0' title='鏂囩珷'>鏂囩珷   涓ユ牸   instance   click   lan   view   arp   

銆€銆€

涓€.寮曡█

         涓庡洓澶х粍浠朵箣涓€鐨凚roadCast骞挎挱姣旇緝锛屽箍鎾富瑕佺洃鍚郴缁熺骇浜嬩欢锛屾瘮濡傜綉缁滃垏鎹紝鐢垫睜鐢甸噺绛夊睘浜庤繘绋嬮棿鐨勯€氫俊锛孍ventBus 鏄繘绋嬪唴鐨勯€氫俊銆?br>         浜嗚ВBroadCast 鍙互鏌ョ湅璇ユ枃绔狅細Android 鍥涘ぇ缁勪欢 (涓? BroadcastReceiver 浠嬬粛

浜?鍩烘湰浣跨敤

銆€銆€寮曞叆绫诲簱:
compile 鈥?/span>com.jakewharton:butterknife:8.5.1鈥?/span>

銆€銆€椤甸潰鎵撳紑鏃跺€欏垵濮嬪寲骞舵敞鍐孍ventBus

//鍒濆鍖?/span>

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
  銆€銆€銆€銆€//娉ㄥ唽 EventBus
  銆€銆€銆€銆€EventBus.getDefault().register(this);
 }

銆€銆€椤甸潰閿€姣佹椂鍊欒В闄ventBus娉ㄥ唽

//閿€姣?EventBus
 @Override
 protected void onDestroy() {
  super.onDestroy();
  EventBus.getDefault().unregister(this);
 }

銆€銆€鍙戝竷浜嬩欢 publish

//鍙戝竷浜嬩欢
 EventBus.getDefault().post(new EventBean("鎴戞潵鑷浜屼釜椤甸潰鐨勮Е鍙?/span>"));

銆€銆€鎺ュ彈鑰?subscribe.娉ㄦ剰鏂规硶鍚嶅彲浠ラ殢渚垮彇,浣嗘槸鍙傛暟绫诲瀷涓巔ublish鍙戝竷鑰呯殑淇濇寔涓€鑷? 杩欓噷鏄疎ventBean

//璁㈤槄鑰呬簨浠讹紝澶勭悊浜嬩欢鍙嶉鐨勬秷鎭?/span>
 @Subscribe
 public void OnEvent(EventBean bean) {
  //鎷垮埌鍙戝竷鑰呬紶杩囨潵鐨別vent浜嬩欢甯︾殑娑堟伅
  String msgStr = bean.getMsg();
  //UI鎵撳嵃鍑烘嬁鍒扮殑娑堟伅
  tvFirst.setText(msgStr);
 }

 

涓?ThreadMode 鍒嗘瀽

銆€銆€浜旂 ThreadMode浠嬬粛銆€

銆€銆€銆€銆€ThreadMode.MAIN 璁㈤槄鑰呭皢鍦ˋndroid鐨勪富绾跨▼锛堟湁鏃剁О涓篣I绾跨▼锛変腑琚皟鐢ㄣ€傚鏋滃彂甯冪嚎绋嬫槸涓荤嚎绋嬶紝鍒欏皢鐩存帴璋冪敤浜嬩欢澶勭悊绋嬪簭鏂规硶锛堜笌ThreadMode.POSTING涓弿杩扮殑鍚屾锛夈€備娇鐢ㄦ妯″紡鐨勪簨浠跺鐞嗙▼搴忓繀椤诲揩閫熻繑鍥炰互閬垮厤闃诲涓荤嚎绋?
銆€銆€銆€銆€ThreadMode.MAIN_ORDERED 璁㈤槄鑰呭皢鍦ˋndroid鐨勪富绾跨▼涓璋冪敤銆傝浜嬩欢鎬绘槸鎺掗槦绛夊緟浠ュ悗浼犻€掔粰璁㈤槄鑰呫€傝繖缁欎簨浠跺鐞嗕竴涓洿涓ユ牸鍜屾洿涓€鑷寸殑椤哄簭锛堝洜姝ゅ悕瀛桵AIN_ORDERED锛夈€備緥濡傦紝濡傛灉浣跨敤MAIN绾跨▼妯″紡鍦ㄤ簨浠跺鐞嗙▼搴忎腑鍙戝竷鍙︿竴涓簨浠讹紝
鍒欑浜屼釜浜嬩欢澶勭悊绋嬪簭灏嗗湪绗竴涓簨浠跺鐞嗙▼搴忎箣鍓嶅畬鎴愶紙鍥犱负瀹冭鍚屾璋冪敤 - 灏嗗叾涓庢柟娉曡皟鐢ㄨ繘琛屾瘮杈冿級銆備娇鐢∕AIN_ORDERED锛岀涓€涓簨浠跺鐞嗙▼搴忓皢瀹屾垚锛岀劧鍚庣浜屼釜灏嗗湪绋嶅悗鐨勬椂闂寸偣琚皟鐢紙鍙涓荤嚎绋嬫湁鑳藉姏锛?
銆€銆€銆€銆€ThreadMode.BACKGROUND 璁㈤槄鑰呭皢鍦ㄥ悗鍙扮嚎绋嬩腑琚皟鐢ㄣ€傚鏋滃彂甯冪嚎绋嬩笉鏄富绾跨▼锛屽垯浼氬湪鍙戝竷绾跨▼涓洿鎺ヨ皟鐢ㄤ簨浠跺鐞嗙▼搴忔柟娉曘€傚鏋滃彂甯冪嚎绋嬫槸涓荤嚎绋嬶紝鍒橢ventBus灏嗕娇鐢ㄤ竴涓悗鍙扮嚎绋嬫潵鎸夐『搴忓彂閫佹墍鏈変簨浠躲€備娇鐢ㄨ繖绉嶆ā寮忕殑浜嬩欢澶勭悊绋嬪簭搴旇灏藉揩杩斿洖浠ラ伩鍏嶉樆濉炲悗鍙扮嚎绋嬨€?銆€銆€銆€銆€ThreadMode.ASYNC 浜嬩欢澶勭悊绋嬪簭鏂规硶鍦ㄥ崟鐙殑绾跨▼涓皟鐢ㄣ€傝繖鎬绘槸鐙珛浜庡彂甯冪嚎绋嬪拰涓荤嚎绋嬨€傚彂甯冧簨浠舵案杩滀笉浼氱瓑寰呬娇鐢ㄦ妯″紡鐨勪簨浠跺鐞嗙▼搴忔柟娉曘€備簨浠跺鐞嗙▼搴忔柟娉曞簲璇ヤ娇鐢ㄨ繖绉嶆ā寮忥紝濡傛灉瀹冧滑鐨勬墽琛屽彲鑳介渶瑕佷竴浜涙椂闂达紝
渚嬪缃戠粶璁块棶銆傞伩鍏嶅悓鏃惰Е鍙戝ぇ閲忛暱鏃堕棿杩愯鐨勫紓姝ュ鐞嗙▼搴忔柟娉曟潵闄愬埗骞跺彂绾跨▼鐨勬暟閲忋€侲ventBus浣跨敤绾跨▼姹犳湁鏁堝湴閲嶇敤宸插畬鎴愬紓姝ヤ簨浠跺鐞嗙▼搴忛€氱煡涓殑绾跨▼銆?銆€銆€銆€銆€ThreadMode.POSTING杩欐槸榛樿鐨勩€傝闃呰€呭皢鍦ㄥ彂甯冭浜嬩欢鐨勫悓涓€涓嚎绋嬩腑琚皟鐢ㄣ€備簨浠朵紶閫掓槸鍚屾瀹屾垚鐨勶紝鎵€鏈夎闃呰€呬竴鏃﹀彂甯冨畬鎴愬氨琚皟鐢ㄣ€傝繖涓猅hreadMode鎰忓懗鐫€鏈€灏忕殑寮€閿€锛屽洜涓哄畠閬垮厤浜嗗畬鍏ㄧ殑绾跨▼鍒囨崲銆?鍥犳锛屽浜庡凡鐭ュ畬鎴愮殑绠€鍗曚换鍔¤€岃█锛岃繖鏄帹鑽愮殑妯″紡锛屽叾鏃堕棿寰堢煭锛屼笉闇€瑕佷富绾跨▼銆備娇鐢ㄨ繖绉嶆ā寮忕殑浜嬩欢澶勭悊绋嬪簭搴旇蹇€熻繑鍥炰互閬垮厤闃诲鍙戝竷绾跨▼锛岃繖鍙兘鏄富绾跨▼銆?

銆€銆€婧愮爜鏌ョ湅涓嬩簲绉峊hreadMode涓嶅悓澶勭悊鏂瑰紡:

private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
        switch (subscription.subscriberMethod.threadMode) {
   銆€銆€銆€銆€銆€銆€//鍙戦€佸拰璁㈤槄鑰呭湪鍚屼竴绾跨▼鍐?/span>
            case POSTING:
                invokeSubscriber(subscription, event);
                break;
   銆€銆€銆€銆€//涓荤嚎绋?/span>
            case MAIN:
                if (isMainThread) {
                    invokeSubscriber(subscription, event);
                } else {
                    mainThreadPoster.enqueue(subscription, event);
                }
                break;
   銆€銆€銆€銆€銆€銆€// 
            case MAIN_ORDERED:
                if (mainThreadPoster != null) {
                    mainThreadPoster.enqueue(subscription, event);
                } else {
                    // temporary: technically not correct as poster not decoupled from subscriber
                    invokeSubscriber(subscription, event);
                }
                break;
   銆€銆€銆€銆€//鍚庡彴锛屼笉鑳藉苟鍙戝鐞?/span>
            case BACKGROUND:
                if (isMainThread) {
                    backgroundPoster.enqueue(subscription, event);
                } else {
                    invokeSubscriber(subscription, event);
                }
                break;
   銆€銆€銆€銆€銆€銆€//瀛愮嚎绋?寮傛骞跺彂澶勭悊 
            case ASYNC:
                asyncPoster.enqueue(subscription, event);
                break;
            default:
                throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
        }
    }
 

鍥?绮樻€т簨浠?/h2>  銆€銆€绠€鍗曡灏辨槸: 鍙戦€佷簨浠朵箣鍚庡啀璁㈤槄璇ヤ簨浠朵篃鑳芥敹鍒拌浜嬩欢銆傚悓鏍稟ndroid 涓篃鏈夊悓鏍风殑绮樻€т簨浠舵満鍒?濡?绮樻€у箍鎾?Sticky Broadcast).

 銆€銆€绮樻€т簨浠朵娇鐢ㄥ満鏅?

銆€銆€涓€浜涗簨浠惰繘琛屼俊鎭劅鍏磋叮鐨勪簨浠跺悗鍙戝竷銆?渚嬪,涓€涓簨浠朵俊鍙?涓€浜涘垵濮嬪寲瀹屾垚銆?鎴栬€呭鏋滀綘鏈変紶鎰熷櫒浣嶇疆鏁版嵁鍜屼綘鎯虫姄浣忔渶杩戠殑鍊笺€?鑰屼笉鏄疄鐜拌嚜宸辩殑缂撳瓨,鎮ㄥ彲浠ヤ娇鐢ㄩ粡鎬х殑浜嬩欢銆?EventBus淇濇寔杩囧幓鐨勪簨浠剁殑鐗瑰畾绫诲瀷鍦ㄥ唴瀛樹腑銆?榛忔€х殑浜嬩欢鍙互浜や粯缁欑敤鎴锋垨鏄惧紡鏌ヨ銆?鍥犳,浣犱笉闇€瑕佷换浣曠壒娈婄殑閫昏緫鏉ヨ€冭檻鍙敤鐨勬暟鎹€?/p>

 銆€銆€绮樻€т娇鐢ㄧず渚嬶細

// 1鍒涘缓涓€涓矘鎬т簨浠剁被
 public class StickyEvent {
  public String msg;
  public StickyEvent(String msg) {
   this.msg = msg;
  }
 }
 // 2 鍙戦€佺矘鎬т簨浠?/span>
 EventBus.getDefault().postSticky(new StickyEvent("鎴戞槸绮樻€т簨浠?/span>"));
銆€銆€// 3 鎺ユ敹绮樻€т簨浠?/span>
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void StickyEventBus(StickyEvent event){
        // 鏄剧ず鎺ユ敹鐨勬暟鎹?/span>
        tv_eventbus_send_result.setText(event.msg);
    }
// 4.鎺ユ敹绮樻€т簨浠舵暟鎹寜閽殑鐐瑰嚮浜嬩欢澶勭悊
 bt_eventbus_send_sticky.setOnClickListener(new View.OnClickListener() {
  銆€銆€@Override
  銆€銆€public void onClick(View v) {
   銆€銆€//true绗竴娆$偣鍑伙紝闃叉閲嶅娉ㄥ唽
   銆€銆€銆€銆€if(isFirstFlag) {
    銆€銆€銆€銆€銆€銆€isFirstFlag = false;
    銆€銆€銆€銆€銆€銆€// 4 娉ㄥ唽
    銆€銆€銆€銆€銆€銆€EventBus.getDefault().register(EventBusSendActivity.this);
   銆€銆€銆€銆€}
  銆€銆€}
 });
@Override
    protected void onDestroy() {
        super.onDestroy();
        // 5 瑙f敞鍐?/span>
        EventBus.getDefault().removeAllStickyEvents();
        EventBus.getDefault().unregister(EventBusSendActivity.this);
    }
 

以上是关于Android 寮€婧愭鏋?( 涓?) 浜嬩欢鎬荤嚎---EventBus的主要内容,如果未能解决你的问题,请参考以下文章

.net寰俊鍏紬鍙峰紑鍙戔€斺€旀秷鎭笌浜嬩欢

Android 鑷畾涔塋istView鍗曞嚮浜嬩欢澶辨晥

JavaScript 浜嬩欢

浜嬩欢鐨勫啋娉″拰榛樿琛屼负

EventEmitter锛歯odeJs浜嬩欢瑙﹀彂鏈哄埗