我有一个服务,我将请求对象传递给它。
final Intent mServiceIntent = new Intent(context, DataManager.class);
final Bundle bundle = new Bundle();
bundle.putParcelable("request", request);
mServiceIntent.putExtras(bundle);
context.startService(mServiceIntent);
上面的代码是调用我的服务并在其中设置了一个包含对象'request'的包。 Request对象包含一个名为'dataVersion'的int变量。现在当我从意图中获得捆绑包时:
this.request = bundle.getParcelable("request");
请求对象中的dataVersion从3变为350,350变为-1。它很少发生,但确实发生了。大多数情况下,我按预期获得捆绑。但是当我不断地快速连续杀死并重新启动服务时,会发生dataVersion对象随机更改其值。
以下是我的parcelable请求:
public int updateId;
public int dataVersion;
public int priority;
public boolean setCurrentCompanyOnUpdate;
public DataUpdateRequest() {
}
public DataUpdateRequest(final int dataVersion, final int priority, final boolean setCurrentCompanyOnUpdate) {
this.updateId = idGen.getAndIncrement();
this.dataVersion = dataVersion;
this.priority = priority;
this.setCurrentCompanyOnUpdate = setCurrentCompanyOnUpdate;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.updateId);
dest.writeInt(this.dataVersion);
dest.writeInt(this.priority);
dest.writeByte(this.setCurrentCompanyOnUpdate ? (byte) 1 : (byte) 0);
}
protected DataUpdateRequest(Parcel in) {
this.updateId = in.readInt();
this.dataVersion = in.readInt();
this.priority = in.readInt();
this.setCurrentCompanyOnUpdate = in.readByte() != 0;
}
public static final Creator<DataUpdateRequest> CREATOR = new Creator<DataUpdateRequest>() {
@Override
public DataUpdateRequest createFromParcel(Parcel source) {
return new DataUpdateRequest(source);
}
@Override
public DataUpdateRequest[] newArray(int size) {
return new DataUpdateRequest[size];
}
};