欠伸展肢体,吟咏心自愉。这篇文章主要讲述Android 寮€婧愭鏋?( 涓?) 浜嬩欢鎬荤嚎---EventBus相关的知识,希望能为你提供帮助。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%96%87%e7%ab%a0' title='鏂囩珷'>鏂囩珷
绠€鍗?/a>
涓ユ牸
浼犻€?/a>
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). 銆€銆€绮樻€т簨浠朵娇鐢ㄥ満鏅?
【Android 寮€婧愭鏋?( 涓?) 浜嬩欢鎬荤嚎---EventBus】銆€銆€涓€浜涗簨浠惰繘琛屼俊鎭劅鍏磋叮鐨勪簨浠跺悗鍙戝竷銆?渚嬪,涓€涓簨浠朵俊鍙?涓€浜涘垵濮嬪寲瀹屾垚銆?鎴栬€呭鏋滀綘鏈変紶鎰熷櫒浣嶇疆鏁版嵁鍜屼綘鎯虫姄浣忔渶杩戠殑鍊笺€?鑰屼笉鏄疄鐜拌嚜宸辩殑缂撳瓨,鎮ㄥ彲浠ヤ娇鐢ㄩ粡鎬х殑浜嬩欢銆?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); }
推荐阅读
- POJ - 3321 Apple Tree(树状数组)
- spring boot 无法读取application.properties问题
- Android padding 和margin
- android N : UnsatisfiedLinkError 只能访问设置为公用库的so库
- android ------ Emulator: error: x86 emulation currently requires hardware acceleration
- Android 关于解决MediaButton学习到的media控制流程
- 安卓监听触摸事件,以及各种按键 处理
- Android静默安装及开机自启的简单实现
- 安卓socket聊天