日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android之EventBus使用详解

發布時間:2025/3/21 Android 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android之EventBus使用详解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、概述

當Android項目越來越龐大的時候,應用的各個部件之間的通信變得越來越復雜,例如:當某一條件發生時,應用中有幾個部件對這個消息感興趣,那么我們通常采用的就是觀察者模式,使用觀察者模式有一個弊病就是部件之間的耦合度太高,在這里我將會詳細介紹Android中的解耦組建EventBus的使用。EventBus是一款針對Android優化的發布/訂閱事件總線。主要功能是替代Intent,Handler,BroadCast在Fragment,Activity,Service,線程之間傳遞消息.優點是開銷小,代碼更優雅。以及將發送者和接收者解耦。

?

1、EvnetBus的下載地址:https://github.com/greenrobot/EventBus.git

?

2、基本使用

(1)自定義一個類,可以是空類,比如:

?

[java]?view plain
  • public?class?AnyEventType?{??
  • ?????public?AnyEventType(){}??
  • ?}??
  • ?

    (2)在要接收消息的頁面注冊:

    ?

    [java]?view plain
  • eventBus.register(this);??
  • ?

    (3)發送消息

    ?

    [java]?view plain
  • eventBus.post(new?AnyEventType?event);??
  • (4)接受消息的頁面實現(共有四個函數,各功能不同,這是其中之一,可以選擇性的實現,這里先實現一個):

    ?

    [java]?view plain
  • public?void?onEvent(AnyEventType?event)?{}??
  • (5)解除注冊

    [java]?view plain
  • eventBus.unregister(this);??
  • 順序就是這么個順序,可真正讓自己寫,估計還是云里霧里的,下面舉個例子來說明下。

    ?

    首先,在EventBus中,獲取實例的方法一般是采用EventBus.getInstance()來獲取默認的EventBus實例,當然你也可以new一個又一個,個人感覺還是用默認的比較好,以防出錯。

    ?

    二、實戰

    先給大家看個例子:

    ?

    當擊btn_try按鈕的時候,跳到第二個Activity,當點擊第二個activity上面的First Event按鈕的時候向第一個Activity發送消息,當第一個Activity收到消息后,一方面將消息Toast顯示,一方面放入textView中顯示。

    按照下面的步驟,下面來建這個工程:

    1、基本框架搭建

    想必大家從一個Activity跳轉到第二個Activity的程序應該都會寫,這里先稍稍把兩個Activity跳轉的代碼建起來。后面再添加EventBus相關的玩意。

    MainActivity布局(activity_main.xml)

    ?

    [html]?view plain
  • <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/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,SecondActivity布局(activity_second.xml)

    [html]?view plain
  • <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"??
  • ????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.java (點擊btn跳轉到第二個Activity)

    [java]?view plain
  • 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)?{??
  • ????????????????//?TODO?Auto-generated?method?stub??
  • ????????????????Intent?intent?=?new?Intent(getApplicationContext(),??
  • ????????????????????????SecondActivity.class);??
  • ????????????????startActivity(intent);??
  • ????????????}??
  • ????????});??
  • ????}??
  • ??
  • }??
  • 到這,基本框架就搭完了,下面開始按步驟使用EventBus了。

    2、新建一個類FirstEvent

    ?

    [java]?view plain
  • package?com.harvic.other;??
  • ??
  • public?class?FirstEvent?{??
  • ??
  • ????private?String?mMsg;??
  • ????public?FirstEvent(String?msg)?{??
  • ????????//?TODO?Auto-generated?constructor?stub??
  • ????????mMsg?=?msg;??
  • ????}??
  • ????public?String?getMsg(){??
  • ????????return?mMsg;??
  • ????}??
  • }??
  • 這個類很簡單,構造時傳進去一個字符串,然后可以通過getMsg()獲取出來。

    ?

    3、在要接收消息的頁面注冊EventBus:

    在上面的GIF圖片的演示中,大家也可以看到,我們是要在MainActivity中接收發過來的消息的,所以我們在MainActivity中注冊消息。

    通過我們會在OnCreate()函數中注冊EventBus,在OnDestroy()函數中反注冊。所以整體的注冊與反注冊的代碼如下:

    ?

    [java]?view plain
  • 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??
  • ????????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)?{??
  • ????????????????//?TODO?Auto-generated?method?stub??
  • ????????????????Intent?intent?=?new?Intent(getApplicationContext(),??
  • ????????????????????????SecondActivity.class);??
  • ????????????????startActivity(intent);??
  • ????????????}??
  • ????????});??
  • ????}??
  • ????@Override??
  • ????protected?void?onDestroy(){??
  • ????????super.onDestroy();??
  • ????????EventBus.getDefault().unregister(this);//反注冊EventBus??
  • ????}??
  • }??
  • 4、發送消息

    發送消息是使用EventBus中的Post方法來實現發送的,發送過去的是我們新建的類的實例!

    ?

    [java]?view plain
  • EventBus.getDefault().post(new?FirstEvent("FirstEvent?btn?clicked"));??
  • 完整的SecondActivity.java的代碼如下:

    ?

    [java]?view plain
  • 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)?{??
  • ????????????????//?TODO?Auto-generated?method?stub??
  • ????????????????EventBus.getDefault().post(??
  • ????????????????????????new?FirstEvent("FirstEvent?btn?clicked"));??
  • ????????????}??
  • ????????});??
  • ????}??
  • }??
  • 5、接收消息

    接收消息時,我們使用EventBus中最常用的onEventMainThread()函數來接收消息,具體為什么用這個,我們下篇再講,這里先給大家一個初步認識,要先能把EventBus用起來先。

    ?

    在MainActivity中重寫onEventMainThread(FirstEvent event),參數就是我們自己定義的類:

    在收到Event實例后,我們將其中攜帶的消息取出,一方面Toast出去,一方面傳到TextView中;

    [java]?view plain
  • 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();??
  • }??
  • 完整的MainActiviy代碼如下:

    ?

    [java]?view plain
  • 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)?{??
  • ????????????????//?TODO?Auto-generated?method?stub??
  • ????????????????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);??
  • ????}??
  • }
  • 轉:http://blog.csdn.net/harvic880925/article/details/40660137?

    轉載于:https://www.cnblogs.com/xijin-wu/p/5293212.html

    《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

    總結

    以上是生活随笔為你收集整理的Android之EventBus使用详解的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。