欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android Intent传递对象的两种方法(Serializable,Parcelable)详细介绍

程序员文章站 2024-02-24 23:11:46
android intent传递对象的两种方法(serializable,parcelable)详细介绍 今天要给大家讲一下android中intent中如何传递对象,就...

android intent传递对象的两种方法(serializable,parcelable)详细介绍

今天要给大家讲一下android中intent中如何传递对象,就我目前所知道的有两种方法,一种是bundle.putserializable(key,object);另一种是bundle.putparcelable(key, object);当然这些object是有一定的条件的,前者是实现了serializable接口,而后者是实现了parcelable接口,为了让大家更容易理解我还是照常写了一个简单的demo,大家就一步一步跟我来吧!

第一步:新建一个android工程命名为objecttrandemo(类比较多哦!)目录结构如下图:

Android Intent传递对象的两种方法(Serializable,Parcelable)详细介绍

第二步:修改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="welcome to mr wei's blog." 
  /> 
<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>  

第三步:新建两个类一个是person.java实现serializable接口,另一个book.java实现parcelable接口,代码分别如下:

person.java:

package com.tutor.objecttran; 
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; 
  } 
   
} 

book.java:

package com.tutor.objecttran; 
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); 
  } 
} 

第四步:修改objecttrandemo.java,并且新建两个activity,一个是objecttrandemo1.java,别一个是objecttrandemo2.java.分别用来显示person对像数据,和book对象数据:,代码分别如下:

objecttrandemo.java:

package com.tutor.objecttran; 
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.tutor.objecttran.ser"; 
  public final static string par_key = "com.tutor.objecttran.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); 
  } 
  //serializeable传递对象的方法 
  public void serializemethod(){ 
    person mperson = new person(); 
    mperson.setname("frankie"); 
    mperson.setage(25); 
    intent mintent = new intent(this,objecttrandemo1.class); 
    bundle mbundle = new bundle(); 
    mbundle.putserializable(ser_key,mperson); 
    mintent.putextras(mbundle); 
     
    startactivity(mintent); 
  } 
  //pacelable传递对象方法 
  public void pacelablemethod(){ 
    book mbook = new book(); 
    mbook.setbookname("android tutor"); 
    mbook.setauthor("frankie"); 
    mbook.setpublishtime(2010); 
    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(); 
    } 
  } 
} 

objecttrandemo1.java:

package com.tutor.objecttran; 
import android.app.activity; 
import android.os.bundle; 
import android.widget.textview; 
public class objecttrandemo1 extends activity { 
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
     
    textview mtextview = new textview(this); 
    person mperson = (person)getintent().getserializableextra(objecttrandemo.ser_key); 
    mtextview.settext("you name is: " + mperson.getname() + "/n"+ 
        "you age is: " + mperson.getage()); 
     
    setcontentview(mtextview); 
  } 
} 

objecttrandemo2.java:

package com.tutor.objecttran; 
import android.app.activity; 
import android.os.bundle; 
import android.widget.textview; 
public class objecttrandemo2 extends activity { 
  
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    textview mtextview = new textview(this); 
    book mbook = (book)getintent().getparcelableextra(objecttrandemo.par_key); 
    mtextview.settext("book name is: " + mbook.getbookname()+"/n"+ 
             "author is: " + mbook.getauthor() + "/n" + 
             "publishtime is: " + mbook.getpublishtime()); 
    setcontentview(mtextview); 
  } 
} 

第五步:比较重要的一步啦,修改androidmanifest.xml文件(将两个新增的activity,objecttrandemo1,objecttrandemo2)申明一下代码如下(第14,15行):

<?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=".objecttrandemo" 
         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=".objecttrandemo1"></activity> 
    <activity android:name=".objecttrandemo2"></activity> 
  </application> 
  <uses-sdk android:minsdkversion="7" /> 
</manifest>  

第六步:运行上述工程查看效果图啦:

效果1:首界面:

Android Intent传递对象的两种方法(Serializable,Parcelable)详细介绍

效果2:点击serializable按钮

Android Intent传递对象的两种方法(Serializable,Parcelable)详细介绍

效果3:点击parcelable按钮:

Android Intent传递对象的两种方法(Serializable,Parcelable)详细介绍

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!