Android實現Parcelable對象序列化的執行個體

來源:互聯網
上載者:User

bundle.putParcelable可以實現傳遞對象,但是這個對象的類必須實現Parcelable介面才能夠使用。

下面是一個簡單的在Activity之間傳遞對象的例子。

parcel實現參數的序列化,把類對象序列化為齊位的二進位流,提高傳輸的效率。

 

 


Parcelable類。最主要的類,也就是我們要傳送的對象的類,需要實現Parcelable介面。


[java]
import android.os.Parcel;   
import android.os.Parcelable;   
   
public class BenParcelable implements Parcelable {   
   
    public String name;   
   
    public int age;   
   
    public String profession;   
   
    public BenParcelable(String name, int age, String profession) {   
        this.name = name;   
        this.age = age;   
        this.profession = profession;   
    }   
   
    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;   
    }   
   
    public String getprofession() {   
        return profession;   
    }   
   
    public void setprofession(String profession) {   
        this.profession = profession;   
    }   
   
    @Override   
    public int describeContents() {   
        return 0;   
    }   
   
    @Override   
    public void writeToParcel(Parcel parcel, int flag) {   
        parcel.writeString(name);   
        parcel.writeInt(age);   
        parcel.writeString(profession);   
    }   
   
    public static final Creator<BenParcelable> CREATOR = new Creator<BenParcelable>() {   
        public BenParcelable createFromParcel(Parcel in) {   
            return new BenParcelable(in);   
        }   
   
        public BenParcelable[] newArray(int size) {   
            return new BenParcelable[size];   
        }   
    };   
   
    private BenParcelable(Parcel in) {   
        name = in.readString();   
        age = in.readInt();   
        profession = in.readString();   
    }   
}   

import android.os.Parcel; 
import android.os.Parcelable; 
 
public class BenParcelable implements Parcelable { 
 
    public String name; 
 
    public int age; 
 
    public String profession; 
 
    public BenParcelable(String name, int age, String profession) { 
        this.name = name; 
        this.age = age; 
        this.profession = profession; 
    } 
 
    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; 
    } 
 
    public String getprofession() { 
        return profession; 
    } 
 
    public void setprofession(String profession) { 
        this.profession = profession; 
    } 
 
    @Override 
    public int describeContents() { 
        return 0; 
    } 
 
    @Override 
    public void writeToParcel(Parcel parcel, int flag) { 
        parcel.writeString(name); 
        parcel.writeInt(age); 
        parcel.writeString(profession); 
    } 
 
    public static final Creator<BenParcelable> CREATOR = new Creator<BenParcelable>() { 
        public BenParcelable createFromParcel(Parcel in) { 
            return new BenParcelable(in); 
        } 
 
        public BenParcelable[] newArray(int size) { 
            return new BenParcelable[size]; 
        } 
    }; 
 
    private BenParcelable(Parcel in) { 
        name = in.readString(); 
        age = in.readInt(); 
        profession = in.readString(); 
    } 

ParcelableActivity類,傳遞對象的Activity類。

 

[java]
import android.app.Activity;   
import android.content.Intent;   
import android.os.Bundle;   
import android.view.View;   
import android.view.View.OnClickListener;   
import android.widget.Button;   
   
public class ParcelableActivity extends Activity {   
   
    private Button myButton;   
   
    @Override   
    public void onCreate(Bundle savedInstanceState) {   
   
        super.onCreate(savedInstanceState);   
   
        setContentView(R.layout.main);   
   
        myButton = (Button) findViewById(R.id.myButton);   
   
        myButton.setOnClickListener(new OnClickListener() {   
   
            @Override   
            public void onClick(View arg0) {   
   
                BenParcelable benParcelable = new BenParcelable("BenZeph", 23,   
                        "Java/Android Engineer");   
   
                Intent intent = new Intent();   
   
                intent.setClass(getApplicationContext(),   
                        GetParcelableActivity.class);   
   
                Bundle bundle = new Bundle();   
   
                bundle.putParcelable("Ben", benParcelable);   
   
                intent.putExtras(bundle);   
   
                startActivity(intent);   
   
            }   
        });   
    }   
}   

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
 
public class ParcelableActivity extends Activity { 
 
    private Button myButton; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
 
        super.onCreate(savedInstanceState); 
 
        setContentView(R.layout.main); 
 
        myButton = (Button) findViewById(R.id.myButton); 
 
        myButton.setOnClickListener(new OnClickListener() { 
 
            @Override 
            public void onClick(View arg0) { 
 
                BenParcelable benParcelable = new BenParcelable("BenZeph", 23, 
                        "Java/Android Engineer"); 
 
                Intent intent = new Intent(); 
 
                intent.setClass(getApplicationContext(), 
                        GetParcelableActivity.class); 
 
                Bundle bundle = new Bundle(); 
 
                bundle.putParcelable("Ben", benParcelable); 
 
                intent.putExtras(bundle); 
 
                startActivity(intent); 
 
            } 
        }); 
    } 

GetParcelableActivity類,接收序列化對象的Activity類。

 

[java]
<SPAN style="FONT-SIZE: 18px"><STRONG>import android.app.Activity;   
import android.os.Bundle;   
   
public class GetParcelableActivity extends Activity {   
    @Override   
    protected void onCreate(Bundle savedInstanceState) {   
   
        super.onCreate(savedInstanceState);   
   
        setContentView(R.layout.main);   
   
        BenParcelable parcelable = getIntent().getParcelableExtra("Ben");   
   
        System.out.println(parcelable.getName());   
   
        System.out.println(parcelable.getAge());   
           
        System.out.println(parcelable.getprofession());   
   
    }   
   
}  </STRONG></SPAN> 

import android.app.Activity; 
import android.os.Bundle; 
 
public class GetParcelableActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
 
        super.onCreate(savedInstanceState); 
 
        setContentView(R.layout.main); 
 
        BenParcelable parcelable = getIntent().getParcelableExtra("Ben"); 
 
        System.out.println(parcelable.getName()); 
 
        System.out.println(parcelable.getAge()); 
         
        System.out.println(parcelable.getprofession()); 
 
    } 
 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.