2
我有一个正在进行网络调用并接收一些JSON数据的IntentService。我将这个响应数据打包在实现parcelable的自定义对象中。如果我将这个可分区对象作为附加对象添加到意图中,然后使用该意图启动一个活动,则一切似乎都按预期工作,即我可以从新创建的活动中从意图中检索可分析对象。但是,如果我从我的IntentService的onHandleIntent()方法内部创建intent,然后使用sendBroadcast(),则广播接收方的onReceive()方法不会触发。但是,如果我没有将意义添加到意图中,那么onReceive()方法会按预期触发。以下是一些相关的代码片段:当意图包含可添加的附加内容时未收到广播
Parcelable对象:
public class JsonResponse implements Parcelable {
private int responseCode;
private String responseMessage;
private String errorMessage;
public JsonResponse() {
}
/*
/ Property Methods
*/
public void setResponseCode(int code) {
this.responseCode = code;
}
public void setResponseMessage(String msg) {
this.responseMessage = msg;
}
public void setErrorMessage(String msg) {
this.errorMessage = msg;
}
/*
/ Parcelable Methods
*/
public static final Creator<JsonResponse> CREATOR = new Creator<JsonResponse>() {
@Override
public JsonResponse createFromParcel(Parcel parcel) {
return new JsonResponse(parcel);
}
@Override
public JsonResponse[] newArray(int i) {
return new JsonResponse[i];
}
};
private JsonResponse(Parcel parcel) {
responseCode = parcel.readInt();
responseMessage = parcel.readString();
errorMessage = parcel.readString();
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(responseCode);
parcel.writeString(responseMessage);
parcel.writeString(errorMessage);
}
@Override
public int describeContents() {
return 0;
}
}
IntentService的onHandle():
protected void onHandleIntent(Intent intent) {
service = new LoginService();
service.login("whoever", "whatever");
JsonResponse response = new JsonResponse();
response.setResponseCode(service.responseCode);
response.setResponseMessage(service.responseMessage);
response.setErrorMessage(service.errorMessage);
Intent i = new Intent();
i.putExtra("jsonResponse", response);
i.setAction(ResultsReceiver.ACTION);
i.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(i);
}
任何想法?任何有识之士将不胜感激。