ネットワーク呼び出しを行ってJSONデータを受信しているIntentServiceがあります。この応答データを、parcelableを実装するカスタムオブジェクトにパッケージ化します。このパーセル可能オブジェクトをエクストラとしてインテントに追加し、そのインテントを使用してアクティビティを起動すると、すべてが期待どおりに機能しているように見えます。つまり、新しく作成されたアクティビティのインテントからパーセル可能オブジェクトを取得できます。ただし、IntentServiceのonHandleIntent()メソッド内からインテントを作成してからsendBroadcast()を使用すると、ブロードキャストレシーバーのonReceive()メソッドは起動しません。ただし、パーセル可能オブジェクトをインテントに追加しない場合、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);
}
何か案は?任意の洞察をいただければ幸いです。