Android教程:通过Intent传递对象的两种方法(Serializable,Parcelable)

在本文中,我将向您展示一个如何在Android应用程序中按意图传递对象的简单示例。 可打包和序列化用于封送和解封Java对象。 在Parcelable中,开发人员编写用于编组和拆组的自定义代码,因此与序列化相比,它创建的垃圾对象更少。 由于采用了这种自定义实现,因此可序列化的Parcelable性能大大提高(快了大约两倍)。

序列化是一个标记界面,这意味着用户无法根据其要求封送数据。 在序列化中,使用Java反射API在Java虚拟机(JVM)上执行封送处理操作。 这有助于识别Java对象的成员和行为,而且最终会创建很多垃圾对象。 因此,与可打包相比,序列化过程较慢。

步骤1:main.xml进行布局

<?xml version="1.0" encoding="utf-8"?>  
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     android:orientation="vertical"  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent"  
     >  
 <TextView    
     android:layout_width="fill_parent"   
     android:layout_height="wrap_content"   
     android:text="Hello Welcome to EasyInfoGeek."  
     />  
 <Button  
     android:id="@+id/button1"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:text="Serializable"  
 />  
 <Button  
     android:id="@+id/button2"  
     android:layout_width="fill_parent"  
     android:layout_height="wrap_content"  
     android:text="Parcelable"  
 />  
 </LinearLayout>

步骤2:创建实现可序列化的Person.java

package com.easyinfogeek.objectPass;  
 import java.io.Serializable;  
 public class Person implements Serializable {  
     private static final long serialVersionUID = -7060210544600464481L;   
     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;  
     }  

 }

步骤3:创建实现Parcelable的Book.java

package  com.easyinfogeek.objectPass;  
 import android.os.Parcel;  
 import android.os.Parcelable;  
 public class Book implements Parcelable {  
     private String bookName;  
     private String author;  
     private int publishTime;  

     public String getBookName() {  
  return bookName;  
     }  
     public void setBookName(String bookName) {  
  this.bookName = bookName;  
     }  
     public String getAuthor() {  
  return author;  
     }  
     public void setAuthor(String author) {  
  this.author = author;  
     }  
     public int getPublishTime() {  
  return publishTime;  
     }  
     public void setPublishTime(int publishTime) {  
  this.publishTime = publishTime;  
     }  

     public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {  
  public Book createFromParcel(Parcel source) {  
      Book mBook = new Book();  
      mBook.bookName = source.readString();  
      mBook.author = source.readString();  
      mBook.publishTime = source.readInt();  
      return mBook;  
  }  
  public Book[] newArray(int size) {  
      return new Book[size];  
  }  
     };  

     public int describeContents() {  
  return 0;  
     }  
     public void writeToParcel(Parcel parcel, int flags) {  
  parcel.writeString(bookName);  
  parcel.writeString(author);  
  parcel.writeInt(publishTime);  
     }  
 }

步骤4:创建ObjectPassDemo.java,它是主要的Activity类Create

另外2个活动类:ObjectPassDemo1.java用于显示对象ObjectPassDemo2.java用于显示书

package com.easyinfogeek.objectPass;  
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 ObjectTranDemo extends Activity implements OnClickListener {  

    private Button sButton,pButton;  
    public  final static String SER_KEY = "com.easyinfogeek.objectPass.ser";  
    public  final static String PAR_KEY = "com.easyinfogeek.objectPass.par";  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);     
        setupViews();  

    }  

    public void setupViews(){  
        sButton = (Button)findViewById(R.id.button1);  
        pButton = (Button)findViewById(R.id.button2);  
        sButton.setOnClickListener(this);  
        pButton.setOnClickListener(this);  
    }  

    public void SerializeMethod(){  
        Person mPerson = new Person();  
        mPerson.setName("Leon");  
        mPerson.setAge(25);  
        Intent mIntent = new Intent(this,ObjectTranDemo1.class);  
        Bundle mBundle = new Bundle();  
        mBundle.putSerializable(SER_KEY,mPerson);  
        mIntent.putExtras(mBundle);  

        startActivity(mIntent);  
    }  

    public void PacelableMethod(){  
        Book mBook = new Book();  
        mBook.setBookName("Android Developer Guide");  
        mBook.setAuthor("Leon");  
        mBook.setPublishTime(2014);  
        Intent mIntent = new Intent(this,ObjectTranDemo2.class);  
        Bundle mBundle = new Bundle();  
        mBundle.putParcelable(PAR_KEY, mBook);  
        mIntent.putExtras(mBundle);  

        startActivity(mIntent);  
    }  

    public void onClick(View v) {  
        if(v == sButton){  
            SerializeMethod();  
        }else{  
            PacelableMethod();  
        }  
    }  
}
package com.easyinfogeek.objectPass;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.widget.TextView;  
 public class ObjectPassDemo1 extends Activity {  
     @Override  
     public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  

  TextView mTextView = new TextView(this);  
  Person mPerson = (Person)getIntent().getSerializableExtra(ObjectPassDemo.SER_KEY);  
  mTextView.setText("You name is: " + mPerson.getName() + "/n"+  
   "You age is: " + mPerson.getAge());  

  setContentView(mTextView);  
     }  
 }
package com.easyinfogeek.objectPass;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.widget.TextView;  
 public class ObjectPassDemo2 extends Activity {  

     public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  TextView mTextView = new TextView(this);  
  Book mBook = (Book)getIntent().getParcelableExtra(ObjectPassDemo.PAR_KEY);  
  mTextView.setText("Book name is: " + mBook.getBookName()+"/n"+  
      "Author is: " + mBook.getAuthor() + "/n" +  
      "PublishTime is: " + mBook.getPublishTime());  
  setContentView(mTextView);  
     }  
 }

步骤5:修改AndroidManifest.xml,添加两个活动性

<?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
       package="com.tutor.objecttran"  
       android:versionCode="1"  
       android:versionName="1.0">  
     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  <activity android:name=".ObjectPassDemo"  
     android:label="@string/app_name">  
      <intent-filter>  
   <action android:name="android.intent.action.MAIN" />  
   <category android:name="android.intent.category.LAUNCHER" />  
      </intent-filter>  
  </activity>  
  <activity android:name=".ObjectPassDemo1"></activity>  
  <activity android:name=".ObjectPassDemo2"></activity>  
     </application>  
     <uses-sdk android:minSdkVersion="7" />  
 </manifest>


翻译自: https://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值