Search code examples
javaandroidandroid-intentserializablereceiver

Casting Serilizable to derivation of HashMap


I am trying to send some work from IntentService to BroadcastReceiver by using .putExtra() and sendBroadcast(), so I have own class called "Message", which extends HashMap< String,String> and implements Serializable.

public class Message extends HashMap<String,String> implements Serializable{
    public MessageID ID;
    public int Encode(byte[] buff,int off);
    public int Decode(byte[] buff,int off);
    //...
}

And I am sending it like this:

public static void ProcessMessage(Message msg) {
    Intent broadcastIntent = new Intent();
    broadcastIntent.setAction(Receiver.BROADCAST);
    broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
    broadcastIntent.putExtra("MESSAGE",(Serializable)msg);
    parentService.sendBroadcast(broadcastIntent);
    Print("Broadcasting intent to receiver ("+Receiver.BROADCAST+") from: "+parentService.toString());
}

And receiving like this:

public void onReceive(Context context, Intent intent) {
    Sys.Print("Receiver handling: "+intent.getAction());
    if(intent.getAction().equals(BROADCAST)){
        try {
            Message msg = (Message) intent.getSerializableExtra("MESSAGE");
            Sys.Print("Receiver handling " + msg.ID.toString());
        } catch(Exception ex){
            Sys.Print("Failed handling message, reason: "+ex.getStackTrace().toString());
        }
    }
}

But here I always get this: "Failed handling message, reason: java.lang.ClassCastException: java.util.HashMap"

Any idea what could be wrong?

Stack-trace:

com.myapp.Receiver.onReceive(Receiver.java:24)
android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
android.os.Handler.handleCallback(Handler.java:587)
android.os.Handler.dispatchMessage(Handler.java:92)
android.os.Looper.loop(Looper.java:138)
android.app.ActivityThread.main(ActivityThread.java:3701)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:507)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
dalvik.system.NativeStart.main(Native Method)

So finally, if anyone had similar problem, I solved it like this:

public class Message implements Parcelable {
    public HashMap<String,String> Data;
    public Message(){
        Data=new HashMap<>();
    }
    public int Encode(byte[] buff,int off);
    public int Decode(byte[] buff,int off);
    public void Add(String i,String v);
    public String At(String i);
    public boolean ContainsKey(String i);
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeMap(this.Data);
    }
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Message createFromParcel(Parcel in) {
            return new Message(in);
        }
        public Message[] newArray(int size) {
            return new Message[size];
        }
    };
    public Message(Parcel in) {
        Data=new HashMap<>();
        in.readMap(this.Data,String.class.getClassLoader());
    }
}

Solution

  • You won't like the answer.

    Extras are stored in a Bundle. Android does some "optimizations" on the content of Bundles and it tries to be smart about how to serialize/deserialize an entry if it knows its type. So if you put anything that implements the Map interface into a Bundle, when you get it back out you will have a HashMap :-(

    See my answer to this question for a detailed explanation of the mechanics.

    To solve your problem, you'll need to have your custom class use (ie: contain) a HashMap and not be (ie: inherit from) a HashMap.