Android中Parcelable的使用

本文详细解析了如何使用Intent和Bundle在Android应用中实现跨进程传输,包括封装对象并进行序列化和反序列化的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、封装要进行跨进程传输的对象

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. package com.lewa;  
  2.   
  3. import android.graphics.Bitmap;  
  4. import android.os.Parcel;  
  5. import android.os.Parcelable;  
  6.   
  7.   
  8. public class WeiBoWidget implements Parcelable{  
  9.       
  10.     private String contentText;  
  11.     private Bitmap contentPicture;  
  12.       
  13.     public WeiBoWidget(){}  
  14.   
  15.     public String getContentText() {  
  16.         return contentText;  
  17.     }  
  18.   
  19.     public void setContentText(String contentText) {  
  20.         this.contentText = contentText;  
  21.     }  
  22.   
  23.     public Bitmap getContentPicture() {  
  24.         return contentPicture;  
  25.     }  
  26.   
  27.     public void setContentPicture(Bitmap contentPicture) {  
  28.         this.contentPicture = contentPicture;  
  29.     }  
  30.   
  31.     @Override  
  32.     public int describeContents() {  
  33.         // TODO Auto-generated method stub  
  34.         return 0;  
  35.     }  
  36.   
  37.     @Override  
  38.     public void writeToParcel(Parcel dest, int flags) {  
  39.         // TODO Auto-generated method stub  
  40.         dest.writeString(contentText);  
  41.         if(this.contentPicture != null){  
  42.             dest.writeInt(1);  
  43.             this.contentPicture.writeToParcel(dest, flags);  
  44.         }else{  
  45.             dest.writeInt(0);  
  46.         }  
  47.     }  
  48.   
  49.     public static final Parcelable.Creator<WeiBoWidget> CREATOR = new Parcelable.Creator<WeiBoWidget>() {  
  50.   
  51.         @Override  
  52.         public WeiBoWidget createFromParcel(Parcel source) {  
  53.             // TODO Auto-generated method stub  
  54.             WeiBoWidget weiBoWidget = new WeiBoWidget();  
  55.             weiBoWidget.contentText = source.readString();  
  56.             if(0 != source.readInt()){  
  57.                 weiBoWidget.contentPicture = Bitmap.CREATOR.createFromParcel(source);//因为Bitmap实现了Parcelable接口,所以这里可以这样使用  
  58.             }  
  59.             return weiBoWidget;  
  60.         }  
  61.   
  62.         @Override  
  63.         public WeiBoWidget[] newArray(int size) {  
  64.             // TODO Auto-generated method stub  
  65.             return new WeiBoWidget[size];  
  66.         }  
  67.     };  
  68.       
  69. }  
package com.lewa; import android.graphics.Bitmap; import android.os.Parcel; import android.os.Parcelable; public class WeiBoWidget implements Parcelable{ private String contentText; private Bitmap contentPicture; public WeiBoWidget(){} public String getContentText() { return contentText; } public void setContentText(String contentText) { this.contentText = contentText; } public Bitmap getContentPicture() { return contentPicture; } public void setContentPicture(Bitmap contentPicture) { this.contentPicture = contentPicture; } @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel dest, int flags) { // TODO Auto-generated method stub dest.writeString(contentText); if(this.contentPicture != null){ dest.writeInt(1); this.contentPicture.writeToParcel(dest, flags); }else{ dest.writeInt(0); } } public static final Parcelable.Creator<WeiBoWidget> CREATOR = new Parcelable.Creator<WeiBoWidget>() { @Override public WeiBoWidget createFromParcel(Parcel source) { // TODO Auto-generated method stub WeiBoWidget weiBoWidget = new WeiBoWidget(); weiBoWidget.contentText = source.readString(); if(0 != source.readInt()){ weiBoWidget.contentPicture = Bitmap.CREATOR.createFromParcel(source);//因为Bitmap实现了Parcelable接口,所以这里可以这样使用 } return weiBoWidget; } @Override public WeiBoWidget[] newArray(int size) { // TODO Auto-generated method stub return new WeiBoWidget[size]; } }; }

2、使用Intent、Bundle进行传输

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. Intent intent = new Intent();  
  2.                 intent.setAction(GET_DATA_OVER);  
  3.                 Bundle extra = new Bundle();  
  4.                 extra.putParcelableArrayList("weiBoWidgets", (ArrayList<WeiBoWidget>) result);  
  5.                 intent.putExtras(extra);  
  6.                 context.sendBroadcast(intent); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值