詳解Android中通過Intent類實現組件間調用的方法_Android

來源:互聯網
上載者:User

Intent是Android中用來調用其它組件的類,通過Intent,我們可以非常方便的調用Activity,Broadcast Receiver和Service。

Intent intent = new Intent(Intent.ACTION_VIEW);intent.setData(Uri.parse("http://www.baidu.com"));startActivity(intent);

上面這段代碼可以用來調用第三方的Activity(啟動第三方瀏覽器來開啟百度首頁)。
Intent有隱式和顯式之分,上面的

Intent intent = new Intent(Intent.ACTION_VIEW);

所建立的intent被稱為隱式Intent。構建隱式Intent需要一個表示action的字串(例如Intent.ACTION_VIEW,其值為" android.intent.action.VIEW"),Android會尋找能夠處理該action的Activity(在manifest檔案中的該Activity下的intent-filter中聲明),並且調用他。
有時候可能多個Activity都聲明能夠處理某一個action,例如:

<activity  android:name=".Activity1">  <intent-filter>    <action android:name="com.abc.def" />    <category android:name="android.intent.category.DEFAULT" />  </intent-filter></activity><activity  android:name=".Activity2">  <intent-filter>    <action android:name="com.abc.def" />    <category android:name="android.intent.category.DEFAULT" />  </intent-filter></activity>

上面Activity1和Activity2都聲明能夠處理“com.abc.def”的action,因此當我們執行以下代碼時

Intent intent = new Intent("com.abc.def");startActivity(intent);

Activity1和Activity2都符合要求,Android將彈出"Complete Action Using"的對話方塊來讓使用者選擇一個要執行的Activity。

值得注意的是,要想能夠匹配隱式Intent的調用,必須包含DEFAULT的category(就是<category android:name="android.intent.category.DEFAULT"/>),而若要匹配顯式Intent,則不需要該category。

對於隱式Intent,除了action之外,還可以提供多種資訊來協助Android選擇首選。還可以添加的其他資訊包括:host,mimeType,path,pathPattern,pathPrefix,port,scheme。

例如為上面Activity2在manifest中的配置添加一個mimeType的屬性:

<activity  android:name=".Activity2">  <intent-filter>    <action android:name="com.abc.def" />    <category android:name="android.intent.category.DEFAULT" />    <data android:mimeType="abc/def"/>  </intent-filter></activity>

那麼:

Intent intent = new Intent("com.abc.def");startActivity(intent);//只有Activity1符合/********************************************/Intent intent = new Intent("com.abc.def");intent.setType("abc/def");startActivity(intent);//只有Activity2符合

如果在建立Intent的時候,指明了要調用的類(例如new Intent(xxActivity.this, xx.class),或者通過setComponent來指定),那麼這樣的Intent被稱為顯示Intent。

對於顯式Intent,因為他已經指明了要調用的具體的類,所以Android會忽略掉其action,category以及data屬性。(個人覺得顯示Intent調用比隱式的更快些)

Serializable vs Parcelable
Android 主要是通過Intent來實現組件之間的相互調用,同時還可以傳遞附加的資料。這些資料主要是儲存在Bundle之中(當調用Intent.putExtras(Bundle)時,Android會複製Bundle中的資料,而不是引用,因此再修改Bundle中的資料並不會改變Intent中攜帶的資料)。

Bundle之中可以存放基礎資料型別 (Elementary Data Type)以及實現了Serializable或Parcelable介面的類。

當我們要向Bundle中存放一個類Obj(包含兩個int成員的簡單類),可以讓它實現Serializable或Parcelable介面,如下所示:

1.Serializable


public class Obj implements Serializable {  private int a;  private int b;  public Obj(int a, int b) {    this.a = a;    this.b = b;  }    @Override  public String toString() {    return "Obj: [" + a + ", " + b + "]";  }}

我們可以通過intent.putExtra("obj", new Obj(1, 2));來將其放入intent中,然後通過 obj = (Obj) intent.getSerializableExtra("obj");來將其取出。
2.Parcelable


public class ObjPar implements Parcelable {  private int a;  private int b;  public ObjPar(int a, int b) {    this.a = a;    this.b = b;  }    @Override  public String toString() {    return "Obj: [" + a + ", " + b + "]";  }  @Override  public int describeContents() {    return 0;  }  @Override  public void writeToParcel(Parcel dest, int flags) {    dest.writeInt(a);    dest.writeInt(b);  }    public static final Parcelable.Creator<ObjPar> CREATOR = new Creator<ObjPar>() {        @Override    public ObjPar[] newArray(int size) {      return new ObjPar[size];    }        @Override    public ObjPar createFromParcel(Parcel source) {            return new ObjPar(source.readInt(), source.readInt());    }  };}

我們可以通過intent.putExtra("objpar", new ObjPar(1, 2));來將其放入intent中,然後通過 objpar =  (ObjPar) intent.getParcelableExtra("objpar"); 來將其取出。
以上是兩種向Bundle中存放Object對象的方法,明顯可以看出實現Serializable介面更加簡單,因為他是一個標記性的介面,並不需要實現某個具體方法。相比而言實現Parcelable介面就顯得相對複雜一些,但這樣帶來的好處是效能的大幅提高。這是因為當我們實現Serializable介面後,真正的序列化工作是由JDK來完成,他需要通過反射來擷取成員變數,因為反射的效能並不高,因此這種序列化方式速度慢。然而實現Parcelable介面時,我們提供了該介面中定義方法的實現(writeToParcel實現序列化,createFromParcel實現還原序列化),這就避免了反射的使用,因此這種方式速度快。

那麼這兩種方式,效能差別有多大呢?下面是國外網站上的一個測試結果:Serializable耗時是Parcelable的10倍左右

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.