ClassCastException异常时,从其他活动结合本地服务异常、ClassCastException

2023-09-03 21:05:58 作者:爱失控¢

在我的应用程序有两个分开的APK。来自第一APK的活性(A1)开始的本地服务,并且能够调用此服务提供的方法。后来活动A1开始从第二APK另一个活动(A2)。在A2的活性会尝试连接到由A1启动本地服务。两种活动运行在同一进程中具有相同的SharedUserID。服务接口被提供为示出本地服务的API的例子。服务的onBind方法返回LocalBinder实例,它具有方法GetService()。 当onServiceConnected A2称为我ClassCastException异常,当我尝试从的IBinder投给MyService.LocalBinder。

在调试器中我可以看到,A2的活性onServiceConnected的服务参数是MyService.LocalBinder的正确实例。我甚至可以看为MyService的调试器的所有属性,但是当我试图投的​​IBinder服务,我得到了ClassCastException异常异常MyService.LocalBinder?有没有一些方法周围或做我必须使用AIDL?

 公共无效onServiceConnected(组件名的className,服务的IBinder)
{

  尝试
  {
       MyService.LocalBinder粘合剂=(MyService.LocalBinder)服务;
       m_IService = binder.getService();
  }
  赶上(ClassCastException异常E)
  {

  }


 }
 
不乐观 31省区市新增本土确诊93例 11,涉11省份 如何应对此轮疫情 张文宏最新发声 疫情还在高危运行期

解决方案

不知道这一点,但我猜你已经编译MyService.LocalBinder成A1和A2的apk。这将产生这种异常的,因为虽然它们具有相同的名称和相同的code,它们仍然是两个独立的类文件。我想你需要的类移动到共享库,使这项工作。

更新(回应评论):我不知道怎样使用接口会有什么不同;作为与类,接口将被实例不同的客户端和服务器上,并因此导致一个转换异常。你也许能够使用反射以解决这个问题,但我不T建议的方式 - 它可以不通过任何AndroidOS或DalvikVM支持

我觉得最好的办法是尝试创建AIDL接口,看看它的工作。这可能是比试图做一些事情,看来Android精心设计,以prevent 10倍更容易。

但是,如果你想继续追求你目前的做法,一个可能路径将是使用的的ClassLoader 加载客户端类到服务器。我不知道这是否可以作出擦出火花。

In my application I have two separated APKs. The Activity (A1) from the first APK starts local service and is able to call methods provided by this Service. Later the activity A1 starts another activity (A2) from the second APK. The A2 activity tries to connect to the local service started by A1. Both activities are running in the same process with the same SharedUserID. Service interface is provided as shown in API examples of LocalService. onBind method of service returns LocalBinder instance which has method getService(). When onServiceConnected of A2 is called I got ClassCastException when I try to cast from IBinder to MyService.LocalBinder.

In the debugger I can see that the service argument of onServiceConnected of A2 activity is the right instance of MyService.LocalBinder. I can even watch all attributes of MyService in debugger, but when I tries to cast the IBinder service to the MyService.LocalBinder I got the ClassCastException exception? Is there some way around or do I have to use AIDL?

public void onServiceConnected(ComponentName className, IBinder service)
{

  try
  {
       MyService.LocalBinder binder = (MyService.LocalBinder)service;
       m_IService = binder.getService();
  }
  catch(ClassCastException e)
  {

  }


 }

解决方案

Not sure about this, but I'm guessing that you have compiled MyService.LocalBinder into both A1 and A2 apks. That would produce this kind of exception, because while they have the same name and the same code, they're still two separate class files. I think you would need to move the class into a shared library to make this work.

Update (responding to comment): I don't see how using an interface would be any different; as with the class, the interface would be instantiated differently on the client and server, and so result in a cast exception. You might be able to use reflection to work around this, but I don't recommend that approach -- it may not be supported by either AndroidOS or the DalvikVM.

I think the best course is to try creating the AIDL interface and see if it does the job. It could be 10x easier than trying to do something that Android seems deliberately designed to prevent.

But if you want to keep pursuing your current approach, one possible path would be to use the ClassLoader to load the client class into the server. I'm not sure if this can be made to work either.

 
精彩推荐
图片推荐