nbe_42
nbe_42

Reputation: 1222

Call methods of a service in the same "shared" process without using AIDL

I'm trying to bind a service from an activity and get possibility to call method of it. These are in different applications (apk) but uses the same sharedUserId and process.

Since they use the same process, Am I obliged to use AIDL or can I use classic IBinder like for Local Service (described on Android Developer sample) ?

I tried both. AIDL works fine and method for Local Service doesn't works, I have an ClassCastException :

E/AndroidRuntime(17511): java.lang.ClassCastException: com.example.app.MyService$LocalBinder cannot be cast to com.example.app.MyService$LocalBinder

Is it possible to use this method for calling service with two apps in a common "shared" process? Or Is that the use of this shared process still requires an IPC method like AIDL?

If I want to use classic IBinder, it's for keeping my application as simple as possible.

Hope you can help me and sorry for my bad english ;-)

Upvotes: 1

Views: 744

Answers (2)

j__m
j__m

Reputation: 9625

Your problem is that each app has its own APK containing its own CLASSES.DEX, and classes and interfaces are not shared between them. App1.apk/com.example.Class1 is considered a different type than App2.apk/com.example.Class1. Whether or not they're identical doesn't matter.

There's a few ways you can address this:

1) As you've noticed, you can use AIDL. This is the least efficient mechanism, and obviously limits you to only AIDL-compatible types.

2) You can use Java reflection API's. This is better than AIDL in terms of efficiency, but of course the syntax isn't that great.

3) You can attempt to use custom class loading to somehow finagle it so that both apps have access to the same type. For what you're trying to do, this is going to be more trouble than it's worth.

Upvotes: 0

Yury
Yury

Reputation: 20936

I guess the problem is the following. Even if you use sharedUserId and your applications run in the same process you cannot call methods of your service locally, because your service and application are in different packages. Thus, you can use only AIDL that will create a proxy in your client application.

Upvotes: 0

Related Questions