I have the folowing method in a Service in my appplication:
public void switchSpeaker(boolean speakerFlag){
if(speakerFlag){
audio_service.setSpeakerphoneOn(false);
}
else{
audio_service.setSpeakerphoneOn(true);
}
}
So my question is whats the best and most effective way to be able to use this method in an Activity like follows
final Button speaker_Button = (Button) findViewById(R.id.widget36);
speaker_Button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
switchSpeaker(true); //method from Service
}
});
Do I have to do an AIDL or is there a simpler way?
It's public, right :)
You can call bindService(Intent) method. Tale a look at ApiDemos, the class LocalServiceBinding.
In the callback method onServiceConnected, you can see:
Now, use the service object (mBoundService) to call the method.
That's all :)
You have to expose service`s switchSpeaker method for clients. Define your .aidl file. Than bind to that service from your activity and simply call switchSpeaker. See documentation
No other simple way to call this method, only if it static)
There are 3 ways to binding service with your activity.
Among these IBinder Implementation is the best suit in your case
Example of IBinder class
1. Server.java Service
2. Client.java Activity
Example of Messanger class
1. Server.java service
2. Client.java Activity
Example of AIDL
1. IRemoteService.aidl
2. Server.java Service
3. Client.java Activity
For more study you can refer this document