欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編程使用Intent傳遞對象的方法分析

 更新時(shí)間:2016年01月12日 12:08:27   作者:殘缺的孤獨(dú)  
這篇文章主要介紹了Android編程使用Intent傳遞對象的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android使用Intent實(shí)現(xiàn)傳遞對象的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例分析了Android編程使用Intent傳遞對象的方法。分享給大家供大家參考,具體如下:

之前的文章中,介紹過Intent的用法,比如啟動活動,發(fā)送廣播,啟發(fā)服務(wù)等,并且可以使用Intent時(shí)傳遞一些數(shù)據(jù)。如下代碼所示:

Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("info", "I am fine");
startActivity(intent);

在傳遞數(shù)據(jù)時(shí),使用的方法是putExtra,支持的數(shù)據(jù)類型有限,如何傳遞對象呢??

在Android中,使用Intent傳遞對象有兩種方式:Serializable序列化方式以及Parcelable串行化方式。

1、Serializable方式

此種方式表示將一個對象轉(zhuǎn)換成可存儲或者可傳輸?shù)臓顟B(tài),序列化后的對象可以在網(wǎng)絡(luò)上進(jìn)行傳輸,可以存儲到本地。

對象序列化,只需要實(shí)現(xiàn)Serializable類。

package com.example.testapplication;
import java.io.Serializable;
/**
 * 對象序列化
 * @author yy
 *
 */
public class Emp implements Serializable {
  private String name;
  private int age;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
}

那么Intent如何傳遞對象參數(shù)呢,查看API發(fā)現(xiàn)如下方法:

復(fù)制代碼 代碼如下:
intent.putExtra(String name, Serializable value);

因此,使用該方法傳遞,如下:

Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("obj", new Emp());
startActivity(intent);

那么如何獲取呢?使用如下方法:

復(fù)制代碼 代碼如下:
Emp emp = (Emp) getIntent().getSerializableExtra("obj");

這樣就獲得了Emp對象了。

2、Parcelable方式

該種方式的實(shí)現(xiàn)原理是將一個完整的對象進(jìn)行分解,使分解的每一部分都是Intent所支持的數(shù)據(jù)類型。示例如下:

package com.example.testapplication;
import android.os.Parcel;
import android.os.Parcelable;
/**
 * Parcelable方式
 * @author yy
 *
 */
public class Emp2 implements Parcelable{
  private String name;
  private int age;
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getAge() {
    return age;
  }
  public void setAge(int age) {
    this.age = age;
  }
  @Override
  public int describeContents() {
    return 0;
  }
  @Override
  public void writeToParcel(Parcel dest, int flag) {
    //寫出name
    dest.writeString(name);
    //寫出age
    dest.writeInt(age);
  }
  public static final Parcelable.Creator<Emp2> creator = new Creator<Emp2>() {
    @Override
    public Emp2[] newArray(int size) {
      return new Emp2[size];
    }
    @Override
    public Emp2 createFromParcel(Parcel source) {
      Emp2 emp2 = new Emp2();
      //讀取的順序要和上面寫出的順序一致
      //讀取name
      emp2.name = source.readString();
      emp2.age = source.readInt();
      return emp2;
    }
  };
}

傳遞對象:方式和序列化相同:

Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("obj", new Emp2());
startActivity(intent);

獲取對象:

復(fù)制代碼 代碼如下:
Emp2 emp2 = getIntent().getParcelableExtra("obj");

3、區(qū)別

Serializable在序列化的時(shí)候會產(chǎn)生大量的臨時(shí)變量,從而引起頻繁的GC。因此,在使用內(nèi)存的時(shí)候,Parcelable 類比Serializable性能高,所以推薦使用Parcelable類。

Parcelable不能使用在要將數(shù)據(jù)存儲在磁盤上的情況,因?yàn)镻arcelable不能很好的保證數(shù)據(jù)的持續(xù)性在外界有變化的情況下。盡管Serializable效率低點(diǎn), 也不提倡用,但在這種情況下,還是建議你用Serializable 。

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論