[Bug][Workaround] in startService / Intent SerializableExtra?

  • 0 Antworten
  • Letztes Antwortdatum
U

u.k-f

Gast
Hallo!

Ich hatte einen interessanten Effekt beim Starten eins Services.

Ich gebe dem Service beim Starten im Intent java.util.Properties mit, um ihm die aktuelle Konfiguration mitzuteilen:

Code:
Properties prop = new Properties();
...
Intent in = new Intent(NetService.SETTINGS_CHANGED,null,getActivity().getApplicationContext(),NetService.class);
in.putExtra(NetService.PROP, prop);
getActivity().startService(in);

Der Service liest die aus dem Intent aus.

Code:
Properties p = (Properties)intent.getSerializableExtra(PROP);

Nun bekam ich eine ClassCastException, java.util.Hasmap cannot be cast to java.util.Properties

Scheinbar wurden die Properties so serialisiert, dass sie beim wieder einlesen als Hashmap interpretiert wurden :scared:

Was genau dahinter steckt, kann ich leider nicht sagen, ich nutze als Workaround die Möglichkeit, Properties in ein byte[] wegzuschreiben und schicke das byte[] als Extra mit:

Code:
Intent in = new Intent(NetService.SETTINGS_CHANGED,null,getActivity().getApplicationContext(),NetService.class);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
	prop.store(baos, "Prop");
}
catch (IOException e) {
	e.printStackTrace();
}
in.putExtra(NetService.PROP, baos.toByteArray());
getActivity().startService(in);

Und so liest der Sevice das byte[] wieder aus, und füllt damit seine Properties:

Code:
byte[] buf = intent.getByteArrayExtra(PROP);
Properties p = new Properties();
try {
	p.load(new ByteArrayInputStream(buf));
}
catch (IOException e) {
	e.printStackTrace();
}

Also, sollte mal jemand ähnliche Probleme haben, so kann man das umgehen...

Grüsse Uwe
 
Zurück
Oben Unten