So, Here is my scenario:
I have two activity and one service –> all three(classes) requires a return type value from another service(so total 2 activity, 2 service in the application).
i.e:
Activity A, starts –> an Activity B and a Service I.
Activity B, starts –> a Service II.
Thereafter, Activity A, B and Service II has to communicate with Service I.
Service I is always running behind and never stops once started.
My approach of doing that was as follows:
-
Write three .aidl files that declares the interface between the four communicating classes.
-
Create Activity A, then create Service I, Activity B;and from Activity B create Service II.
-
As Service I can have only one onBind() method, for communicating with the 3 classes at any time, what i’m doing is that all intent from 3 classes are differentiated by assigning different value using setType() property.
-
Connection is built between each two classes when communication has to be done using initService() and released soon after that using releaseService().
-
At Service I, the onBind() method has to first check from which of the 3 classes the intent is coming, using getType() property, and return the respective IBinder type.
Now,
my problem is at step 5. in which the onBind() method can’t perform the operation of checking the getType() of every intents and give the respective return type.
Here is my code, for onBinder() method in Service I:
@Override
public IBinder onBind(final Intent intent){
Log.d(Main_Service.TAG,"it got connected");
if(intent.getType=="Activity_A")
{
return new Activity_A_aidl.Stub() {
public void function1(int u1,String p1,String p2) throws RemoteException{
//do something
}
};}
if(intent.getType=="Activity_B"){
return new Activity_B_aidl.Stub() {
public int function2() throws RemoteException{//do something
return some_integer_values;
}
};}
if(intent.getType()="Service_II"){
return new Service_II_aidl.Stub(){
public boolean function3(String u,String p) throws RemoteException{
//do something
return boolean_type_variable;
}
};}
}
error: as show in LogCat: java.lang.NullPointerException
from all 3 classes(Activity A,B & Service II) where-ever i’m calling one of the functions within try-catch block.
Can anyone tell me where am i going wrong.
-Kishore Debnath, (3rd yr, CSE Student).
Without commenting on your overall design, I would add that the way for the service to differentiate between callers is to use the ‘action’ property. I don’t know what initService() is but when I bind to a remote service, I simply use bindService(). So in your activities have something like:
or .ACTION_2 or .ACTION_3.
Then in your services manifest have an intent filter:
Then in the service’s onBind(), use intent.getAction() to return a String, identifying the caller. (Use the proper .equals() method to match the String!) and return the appropriate IBinder
I don’t think extras on the Intent get passed through to the service. I’ve never bound to service from another service but I suppose it might work.
As a final point, I only use AIDL and a remote service because for commercial reasons I need the functionality to be split between two separate .apks. There is no good technical reason for me to use it, in fact quite the opposite.