Android之Parcelable使用
必须实现Parcelable:且属性的writeXXX与readXXX必须顺序一一对应。
public class BaseModel implements Parcelable {
public long mID;
public long getmID() {
return mID;
}
public void setmID(long mID) {
this.mID = mID;
}
@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.writeLong(mID);
}
public static final Parcelable.Creator<BaseModel> CREATOR = new Parcelable.Creator<BaseModel>() {
//重写Creator
@Override
public BaseModel createFromParcel(Parcel source) {
BaseModel p = new BaseModel();
p.mID = source.readLong();
return p;
}
@Override
public BaseModel[] newArray(int size) {
// TODO Auto-generated method stub
return new BaseModel[size];
}
};
}
发送广播:
public static void sendList(Context mContext,ArrayList<MusicModel> list){
Intent intent = new Intent("PLAYLIST");
intent.putParcelableArrayListExtra(MediaService.MediaServicePlayList, list);
mContext.sendBroadcast(intent);
}
接收广播:
public class PlayListReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
mList = intent.getParcelableArrayListExtra(MediaServicePlayList);
}
}
注册广播:
playListReceiver = new PlayListReceiver();
registerReceiver(playListReceiver,new IntentFilter("PLAYLIST"));