java - Sending serializable object between applications with broadcast receiver -
im trying send object implements "serializable" 1 application via broadcast receiver.
i want send class like:
public class myobject implements serializable{ //basic properties }
in application this:
intent = new intent("myapplication.string"); bundle b = new bundle(); b.putserializable("myclass",obj); i.putextras(b); sendbroadcast(i);
when debug can verify object stored in bundle in intent.
in application b this:
manifest
<receiver android:name="com.example.myapplication.myreceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="myapplication.string"/> </intent-filter> </receiver>
broadcast receiver
@override public void onreceive(context context, intent intent) { try{ bundle b = intent.getextras(); myobject s = (myobject)b.getserializable("myclass"); }catch (exception e){ log.d(tag,e.getmessage()); } }
in application b intent not hold data did in application a.
when try cast data throws an:
parcelable encountered classnotfoundexception reading serializable object (name = com.example.myapplication.myobject)
i've copied class implementation in both application match.
in application data in mmap property of intent.extras - in app b empty.
can me understand this?
thanks.
im trying send object implements "serializable" 1 application via broadcast receiver.
that not idea. requires both apps have same class definition java class, or @ least 1 compatible. since apps may not updated @ same time, can run cases sender has newer edition of java class recipient, can cause deserialization exception.
in application this:
that not idea. not sending data other app. sending data app cares listen broadcast, including may want spy on output.
in application b this:
that not idea. app can send broadcast app, either spoof messages or attempt cause app crash. , on android 8.0+, not receive broadcast in case.
i've copied class implementation in both application match.
perhaps there problem in how did this, error message seem disagree assessment.
i start getting rid of serializable
. put things extras guaranteed recognized correctly parties accessing extras. so, use simple primitives, or bundle
, or other framework classes, not custom serializable
or parcelable
implementations. then, see if more ordinary extras making app app.
then, better job of inter-process communication:
use explicit
intent
(onecomponentname
), not implicitintent
(one action string), "broadcast" goes between 2 parties , works around android 8.0+ implicit broadcast restrictionseither implement permissions (e.g.,
android:permission
on<receiver>
) or perform signature checks ensure 2 parties think are
Comments
Post a Comment