现在的位置: 首页 > 综合 > 正文

android之Intent复杂数据的传递二(Object类型的数据)

2018年06月09日 ⁄ 综合 ⁄ 共 3574字 ⁄ 字号 评论关闭

使用Parcelable方式

前提:Object需要实现Parcelable接口

Parcelable方式传递Object的语法:bundle.putParcelable(key,object);
Parcelable方式接收Object的语法:object=(Object) getIntent().getParcelableExtra(key);
实现Parcelable接口的类比较复杂,Parcelable是个什么东西呢?
Android提供了一种新的类型:Parcel,被用作封装数据的容器,封装后的数据可以通过IntentIPC传递。 除了基本类型以外,只有实现了Parcelable接口的类才能被放入Parcel中。
实现Parcelable接口需要实现三个方法: 

1writeToParcel方法。该方法将类的数据写入外部提供的Parcel中。
声明:writeToParcel(Parcel dest, int flags)
2describeContents方法。直接返回0就可以。

3静态的Parcelable.Creator<T>接口,本接口有两个方法:createFromParcel(Parcel in) 实现从in中创建出类的实例的功能。
 newArray(int size) 创建一个类型为T,长度为size的数组, returnnew T[size];即可。本方法是供外部类反序列化本类数组使用。

1)首先在创建的项目工程里建立一个实现了Parcelable接口的PersonInfo实体类,然后在其中定义若干属性并生成相应的setget方法,具体代码如下:

package zjh.android.bean;
 
import android.annotation.SuppressLint;
import android.os.Parcel;
import android.os.Parcelable;
 
@SuppressLint("ParcelCreator")
public class PersonInfo implements Parcelable{
private String name;
private String address;
private int age;
public PersonInfo() {
}
 
public PersonInfo(String name,String address,int age){
this.name = name;
this.address = address;
this.age = age;
}
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public String getAddress() {
return address;
}
 
public void setAddress(String address) {
this.address = address;
}
 
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 flags) {
dest.writeString(name);
dest.writeString(address);
dest.writeInt(age);
}
 
public static final Parcelable.Creator<PersonInfo> CREATOR = new Creator<PersonInfo>() {
@Override
public PersonInfo[] newArray(int size) {
return new PersonInfo[size];
}
@Override
public PersonInfo createFromParcel(Parcel source) {
PersonInfo personInfo = new PersonInfo();
personInfo.name = source.readString();
personInfo.address = source.readString();
personInfo.age = source.readInt();
return personInfo;
}
};
}

2)建立一个用来发送数据的SendActivity类,代码如下:

package zjh.android.lx;
 
import zjh.android.bean.PersonInfo;
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 SendActivity extends Activity {
private Button send;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.send = (Button)super.findViewById(R.id.send);
this.send.setOnClickListener(new OnClickListenerImpl());
}
private final class OnClickListenerImpl implements OnClickListener{
 
@Override
public void onClick(View v) {
PersonInfo personInfo = new PersonInfo("张三","广州",22);
Intent intent = new Intent(SendActivity.this,ReceiveActivity.class);
Bundle bundle = new Bundle();
//调用writeToParcel()方法,向dest写数据
bundle.putParcelable("personInfo", personInfo);
intent.putExtras(bundle);
SendActivity.this.startActivity(intent);
}
}
 
}
 

3)建立一个用来接收Parcelable类型的复杂数据,具体代码如下:

package zjh.android.lx;
 
import zjh.android.bean.PersonInfo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class ReceiveActivity extends Activity {
private TextView msg;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.receive);
this.msg = (TextView) super.findViewById(R.id.msg);
Intent intent = super.getIntent();
// 这里调用了createFromParcel()方法,返回Parcel实例
PersonInfo personInfo = intent.getParcelableExtra("personInfo");
this.msg
.setText("name=" + personInfo.getName() + "\n" + "address="
+ personInfo.getAddress() + "\n" + "age="
+ personInfo.getAge());
}
 
}

4)在AndroidManifest.xml文件中添加用来接收复杂数据的Activity如下所示:

 <activity android:name="zjh.android.lx.ReceiveActivity"/>
【上篇】