我有一个intentservice,它正在进行网络调用并接收一些json数据。我将此响应数据打包到实现Parcelable的自定义对象中。如果我将此Parcelable对象作为额外对象添加到一个意图,然后使用该意图启动一个活动,则一切似乎都按预期工作,即我可以从新创建的活动中的意图检索Parcelable。但是,如果我从intentservice的onhandleIntent()方法中创建意图,然后使用sendBroadcast(),则广播接收器的onReceive()方法永远不会触发。但是,如果我不将parcelable添加到意图中,onReceive()方法将按预期触发。以下是一些相关的代码片段:
可分块对象:

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);
}

有什么想法吗?任何洞察都将不胜感激。

最佳答案

这个问题似乎与作为额外对象添加的对象的大小有关。当响应对象的一个字符串属性太大时,广播显然失败。我没有消息来源来证实这一点,只有一些尝试和错误,在操作其中一个字符串,同时保留方程常数的所有其他变量。

10-07 19:39
查看更多