在Android中通过intent只能传递基本类型的数据,如果想要传递自定义的javabean,必须实现Serializable或者Parcelable接口。Parcelable接口是在Android新引入的接口。使用方法如下:
- public class DownloadItem implements Parcelable{
- private String url;
- private String gameName;
- private String gamePkgName;
- private int total_bytes;
- private int current_bytes;
- private String path;
- public DownloadItem(){};
- public DownloadItem(String url, String gameName, String gamePkgName,
- int total_bytes, int current_bytes, String path) {
- super();
- this.url = url;
- this.gameName = gameName;
- this.gamePkgName = gamePkgName;
- this.total_bytes = total_bytes;
- this.current_bytes = current_bytes;
- this.path = path;
- }
- @Override
- public String toString() {
- return "DownloadItem [url=" + url + ", gameName=" + gameName
- + ", gamePkgName=" + gamePkgName + ", total_bytes="
- + total_bytes + ", current_bytes=" + current_bytes + ", path="
- + path + "]";
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getGameName() {
- return gameName;
- }
- public void setGameName(String gameName) {
- this.gameName = gameName;
- }
- public String getGamePkgName() {
- return gamePkgName;
- }
- public void setGamePkgName(String gamePkgName) {
- this.gamePkgName = gamePkgName;
- }
- public int getTotal_bytes() {
- return total_bytes;
- }
- public void setTotal_bytes(int total_bytes) {
- this.total_bytes = total_bytes;
- }
- public int getCurrent_bytes() {
- return current_bytes;
- }
- public void setCurrent_bytes(int current_bytes) {
- this.current_bytes = current_bytes;
- }
- public String getPath() {
- return path;
- }
- public void setPath(String path) {
- this.path = path;
- }
- @Override
- public int describeContents() {
- // TODO Auto-generated method stub
- return 0;
- }
- @Override
- public void writeToParcel(Parcel dest, int flags) {
- dest.writeString(this.url);
- dest.writeString(this.gameName);
- dest.writeString(this.gamePkgName);
- dest.writeInt(this.total_bytes);
- dest.writeInt(this.current_bytes);
- dest.writeString(this.path);
- }
- public static final Parcelable.Creator<DownloadItem> CREATOR = new Parcelable.Creator<DownloadItem>() {
- @Override
- public DownloadItem createFromParcel(Parcel source) {
- return new DownloadItem(source.readString(),source.readString(),source.readString(),source.readInt(),source.readInt(),source.readString());
- }
- @Override
- public DownloadItem[] newArray(int size) {
- return new DownloadItem[size];
- }
- };
- }