一.EventBus概述
公司主營業(yè)務(wù):成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、移動(dòng)網(wǎng)站開發(fā)等業(yè)務(wù)。幫助企業(yè)客戶真正實(shí)現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。創(chuàng)新互聯(lián)建站是一支青春激揚(yáng)、勤奮敬業(yè)、活力青春激揚(yáng)、勤奮敬業(yè)、活力澎湃、和諧高效的團(tuán)隊(duì)。公司秉承以“開放、自由、嚴(yán)謹(jǐn)、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。創(chuàng)新互聯(lián)建站推出紅安免費(fèi)做網(wǎng)站回饋大家。
1.EventBus的三要素
EventBus有三個(gè)主要的元素需要我們先了解一下:
Event:事件,可以是任意類型的對象。
Subscriber:事件訂閱者,在EventBus3.0之前消息處理的方法只能限定于onEvent、onEventMainThread、onEventBackgroundThread和onEventAsync,他們分別代表四種線程模型。而在EventBus3.0之后,事件處理的方法可以隨便取名,但是需要添加一個(gè)注解@Subscribe,并且要指定線程模型(默認(rèn)為POSTING),四種線程模型下面會(huì)講到。
Publisher:事件發(fā)布者,可以在任意線程任意位置發(fā)送事件,直接調(diào)用EventBus的post(Object)方法??梢宰约簩?shí)例化EventBus對象,但一般使用EventBus.getDefault()就好了,根據(jù)post函數(shù)參數(shù)的類型,會(huì)自動(dòng)調(diào)用訂閱相應(yīng)類型事件的函數(shù)。
2.EventBus的四種ThreadMode(線程模型)
EventBus3.0有以下四種ThreadMode:
POSTING(默認(rèn)):如果使用事件處理函數(shù)指定了線程模型為POSTING,那么該事件在哪個(gè)線程發(fā)布出來的,事件處理函數(shù)就會(huì)在這個(gè)線程中運(yùn)行,也就是說發(fā)布事件和接收事件在同一個(gè)線程。在線程模型為POSTING的事件處理函數(shù)中盡量避免執(zhí)行耗時(shí)操作,因?yàn)樗鼤?huì)阻塞事件的傳遞,甚至有可能會(huì)引起ANR。
MAIN:事件的處理會(huì)在UI線程中執(zhí)行。事件處理時(shí)間不能太長,長了會(huì)ANR的。
BACKGROUND:如果事件是在UI線程中發(fā)布出來的,那么該事件處理函數(shù)就會(huì)在新的線程中運(yùn)行,如果事件本來就是子線程中發(fā)布出來的,那么該事件處理函數(shù)直接在發(fā)布事件的線程中執(zhí)行。在此事件處理函數(shù)中禁止進(jìn)行UI更新操作。
ASYNC:無論事件在哪個(gè)線程發(fā)布,該事件處理函數(shù)都會(huì)在新建的子線程中執(zhí)行,同樣,此事件處理函數(shù)中禁止進(jìn)行UI更新操作。
二.EventBus的基本用法
1.自定義一個(gè)事件類(相當(dāng)于我們平常所用的bean類)
public class MessageEvent { ... }
2.在需要訂閱的地方注冊
EventBus.getDefault().register(this);
3.發(fā)送事件
第一種.普通事件
EventBus.getDefault().post(messageEvent);
第二種.粘性事件
EventBus.getDefault().postSticky(messageEvent);
4.處理事件(eg.刷新UI)
@Subscribe(threadMode = ThreadMode.MAIN) public void XXX(MessageEvent messageEvent) { ... }
5.取消事件訂閱
@Override protected void onDestroy() { EventBus.getDefault().unregister(this); super.onDestroy(); }
三.EventBus的實(shí)際應(yīng)用(模擬登陸傳值)
1.導(dǎo)入3.0依賴
compile 'org.greenrobot:eventbus:3.0.0'
2.定義消息事件類
public class MessageEvent { public final String uname; public final String upass; public MessageEvent(String name,String pass) { this.uname = name; this.upass = pass; } }
3.發(fā)送事件(粘性事件)
public class MainActivity extends AppCompatActivity { private String username; private String password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextInputLayout usernameWrapper = (TextInputLayout) findViewById(R.id.usernameWrapper); final TextInputLayout passwordWrapper = (TextInputLayout) findViewById(R.id.passwordWrapper); Button btn = (Button) findViewById(R.id.btn); usernameWrapper.setHint("請輸入賬號(hào)"); passwordWrapper.setHint("請輸入密碼"); //點(diǎn)擊事件 btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { hideKeyboard(); username = usernameWrapper.getEditText().getText().toString(); password = passwordWrapper.getEditText().getText().toString(); if (!validateEmail(username)) { usernameWrapper.setError("Not a valid email address!"); } else if (!validatePassword(password)) { passwordWrapper.setError("Not a valid password!"); } else { usernameWrapper.setErrorEnabled(false); passwordWrapper.setErrorEnabled(false); //發(fā)送粘性事件////////////////// EventBus.getDefault().postSticky(new MessageEvent(username,password)); startActivity(new Intent(MainActivity.this,SecondActivity.class)); } } }); } private void hideKeyboard() { View view = getCurrentFocus(); if (view != null) { ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)). hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } } //郵箱驗(yàn)證 public boolean validateEmail(String email) { return email.length() > 5; } // 密碼驗(yàn)證 public boolean validatePassword(String password) { return password.length() > 5; } }
4.注冊和取消訂閱事件
public class SecondActivity extends AppCompatActivity { private TextView name,pass; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); name= (TextView) findViewById(R.id.uname); pass= (TextView) findViewById(R.id.upass); button= (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //注冊EventBus EventBus.getDefault().register(SecondActivity.this); } }); } //事件訂閱者處理事件 @Subscribe(threadMode = ThreadMode.POSTING,sticky = true) public void onUserEvent(MessageEvent event) { name.setText("用戶名:" + event.uname); pass.setText("用戶名:" + event.upass); } //取消注冊 @Override protected void onDestroy() { EventBus.getDefault().unregister(this); super.onDestroy(); } }
布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" 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" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="Welcome" android:textSize="30sp" android:textColor="#333333"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0.5" android:orientation="vertical"> <android.support.design.widget.TextInputLayout android:id="@+id/usernameWrapper" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:hint="Username"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:id="@+id/passwordWrapper" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/usernameWrapper" android:layout_marginTop="4dp"> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="Password"/> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/btn" android:layout_marginTop="4dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login"/> </LinearLayout> </LinearLayout>
activity_second.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_second" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center"> <Button android:id="@+id/button" android:layout_width="100dp" android:layout_height="match_parent" android:text="接收數(shù)據(jù)" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/uname" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/upass" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站欄目:android事件總線EventBus3.0使用方法詳解
本文地址:http://www.muchs.cn/article30/pieipo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、標(biāo)簽優(yōu)化、定制開發(fā)、網(wǎng)站設(shè)計(jì)、網(wǎng)站維護(hù)、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)