带包裹的意图如何在两个应用程序之间传递?

时间:2019-02-27 13:20:34

标签: android android-intent parcelable

我在android app2中有服务。我想从app1启动Service,然后将Intent中的Album对象传递给app2中的Service。

在app1中

var alb: Album = Album()
alb.name="album name"
alb.numOfSongs=1
alb.thumbnail=2

var serviceIntent: Intent = Intent();
var componentName: ComponentName = ComponentName("com.eusecom.demoad","com.eusecom.demoad.view.EkasaService")
serviceIntent.setComponent(componentName)
serviceIntent.putExtra("albumpar", alb)
context.startService(serviceIntent)

在服务类的app2中

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Album alb = intent.getParcelableExtra("albumpar");

}

我可以在Intent中传递String。如果我将“可打包”置于“意图”中,则会出错。

  

02-27 14:12:18.438 22198-22198 / com.eusecom.demoad E /宗地:解组时找不到类:com.eusecom.samshopersung.models.Album                                                               java.lang.ClassNotFoundException:com.eusecom.samshopersung.models.Album                                                                   在java.lang.Class.classForName(本机方法)                                                                   在java.lang.Class.forName(Class.java:400)                                                                   在android.os.Parcel.readParcelableCreator(Parcel.java:2517)                                                                   在android.os.Parcel.readParcelable(Parcel.java:2471)                                                                   在android.os.Parcel.readValue(Parcel.java:2374)                                                                   在android.os.Parcel.readArrayMapInternal(Parcel.java:2727)                                                                   在android.os.BaseBundle.unparcel(BaseBundle.java:269)                                                                   在android.os.BaseBundle.getString(BaseBundle.java:992)                                                                   在android.content.Intent.getStringExtra(Intent.java:6743)                                                                   在com.eusecom.demoad.view.EkasaService.onStartCommand(EkasaService.java:29)                                                                   在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3685)                                                                   在android.app.ActivityThread.-wrap23(ActivityThread.java)                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1740)                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                   在android.os.Looper.loop(Looper.java:154)                                                                   在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                   在java.lang.reflect.Method.invoke(本机方法)                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)                                                                原因:java.lang.ClassNotFoundException:在路径:DexPathList [[zip文件“ /data/app/com.eusecom.demoad-1/base.apk”中找不到类“ com.eusecom.samshopersung.models.Album”。 “],nativeLibraryDirectories = [/ data / app / com.eusecom.demoad-1 / lib / arm64,/ system / lib64,/ vendor / lib64]]]                                                                   在dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)                                                                   在java.lang.ClassLoader.loadClass(ClassLoader.java:380)                                                                   在java.lang.ClassLoader.loadClass(ClassLoader.java:312)                                                                   在java.lang.Class.classForName(本机方法)                                                                   在java.lang.Class.forName(Class.java:400)                                                                   在android.os.Parcel.readParcelableCreator(Parcel.java:2517)                                                                   在android.os.Parcel.readParcelable(Parcel.java:2471)                                                                   在android.os.Parcel.readValue(Parcel.java:2374)                                                                   在android.os.Parcel.readArrayMapInternal(Parcel.java:2727)                                                                   在android.os.BaseBundle.unparcel(BaseBundle.java:269)                                                                   在android.os.BaseBundle.getString(BaseBundle.java:992)                                                                   在android.content.Intent.getStringExtra(Intent.java:6743)                                                                   在com.eusecom.demoad.view.EkasaService.onStartCommand(EkasaService.java:29)                                                                   在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3685)                                                                   在android.app.ActivityThread.-wrap23(ActivityThread.java)                                                                   在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1740)                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                   在android.os.Looper.loop(Looper.java:154)                                                                   在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                   在java.lang.reflect.Method.invoke(本机方法)                                                                   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)   02-27 14:12:18.439 22198-22198 / com.eusecom.demoad D / AndroidRuntime:关闭VM   02-27 14:12:18.440 22198-22198 / com.eusecom.demoad E / AndroidRuntime:FATAL EXCEPTION:main                                                                       流程:com.eusecom.demoad,PID:22198                                                                       java.lang.RuntimeException:无法使用意图{cmp = com.eusecom.demoad / .view.EkasaService launchParam = MultiScreenLaunchParams {mDisplayId = 0 mFlags = 0}(具有附加功能)启动服务com.eusecom.demoad.view.EkasaService@fe9af21 }:android.os.BadParcelableException:解组时的ClassNotFoundException:com.eusecom.samshopersung.models.Album                                                                           在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3702)                                                                           在android.app.ActivityThread.-wrap23(ActivityThread.java)                                                                           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1740)                                                                           在android.os.Handler.dispatchMessage(Handler.java:102)                                                                           在android.os.Looper.loop(Looper.java:154)                                                                           在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                           在java.lang.reflect.Method.invoke(本机方法)                                                                           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1468)                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)                                                                        引起原因:android.os.BadParcelableException:解组时的ClassNotFoundException:com.eusecom.samshopersung.models.Album                                                                           在android.os.Parcel.readParcelableCreator(Parcel.java:2545)                                                                           在android.os.Parcel.readParcelable(Parcel.java:2471)                                                                           在android.os.Parcel.readValue(Parcel.java:2374)                                                                           在android.os.Parcel.readArrayMapInternal(Parcel.java:2727)                                                                           在android.os.BaseBundle.unparcel(BaseBundle.java:269)                                                                           在android.os.BaseBundle.getString(BaseBundle.java:992)                                                                           在android.content.Intent.getStringExtra(Intent.java:6743)                                                                           在com.eusecom.demoad.view.EkasaService.onStartCommand(EkasaService.java:29)                                                                           在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3685)                                                                           在android.app.ActivityThread.-wrap23(ActivityThread.java)                                                                           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1740)                                                                           在android.os.Handler.dispatchMessage(Handler.java:102)                                                                           在android.os.Looper.loop(Looper.java:154)                                                                           在android.app.ActivityThread.main(ActivityThread.java:6692)                                                                           在java.lang.reflect.Method.invoke(本机方法)

1 个答案:

答案 0 :(得分:0)

最好将对象序列化为字符串,将字符串传递到Intent中,然后将字符串反序列化为目标应用程序中的对象。