android Parcelable序列化数组String[]
16lz
2021-01-23
package com.semisky.midLevel.aidl;import android.os.Parcel;import android.os.Parcelable;import android.util.Log;public class AutoIPCMessage implements Parcelable {private static final String TAG = "Service-->AutoIPCMessage";public static final String BROADCAST = "";public String mSrcPackageName; //发起该消息的app包名public String mDstPackageName; //接收该消息的app包名,如果为空,表示广播public int mAction; //发起的动作意图public String[] mParam = null; //实际参数public AutoIPCMessage(String srcPackageName,String dstPackageName,int action,String[] param) {mSrcPackageName = srcPackageName;mDstPackageName = dstPackageName;mAction = action;mParam = param;}@Overridepublic int describeContents() {// TODO Auto-generated method stubreturn 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {//这里是把对象的字段一个一个写到流里面,写的顺序要和下面读的顺序一摸一样Log.d(TAG,"writeToParcel " + this.toString());dest.writeString(mSrcPackageName);dest.writeString(mDstPackageName);dest.writeInt(mAction);//这几句话是写数组的,因为数组的长度不确定,所以先确定数组长度,如果为空就不写,但是要把0给发过去 //让下面的好判断能不能读数组,也就是说下面如果读到的长度是0,那么就不读数组了,否则就创建相同长度的数组去读if(mParam == null) {dest.writeInt(0);} else {dest.writeInt(mParam.length);dest.writeStringArray(mParam);}}/* * 反序列化,开始读对象的流顺序要和上面写的一样 */private AutoIPCMessage(Parcel source) {Log.d(TAG,"AutoIPCMessage read");mSrcPackageName = source.readString();mDstPackageName = source.readString();mAction = source.readInt();//开始读数组的长度int length = source.readInt();//如果数组长度大于0,那么就读数组, 所有数组的操作都可以这样。 if(length>0){ mParam = new String[length]; source.readStringArray(mParam); } }public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {@Overridepublic AutoIPCMessage createFromParcel(Parcel source) {return new AutoIPCMessage(source);}@Overridepublic AutoIPCMessage[] newArray(int size) {return new AutoIPCMessage[size];}};}
更多相关文章
- Blog-07-《一周快速上手Kotlin For Android》-之数组
- 【Android】TextView的文字长度测量及各种padding解析
- js将一个数组传给android
- 设置TextView超过一定长度后显示省略号
- Android最简单的使用数组的适配器Adapter
- Android EditText输入限制最大字符长度 和 限制只能输入数字和字