Search code examples
androidaidl

Aidl result via callback


I have written an application which records some audio signal. This signal has to be processed by a plug-in which is installed on my device. For the communication between the main application and the plug-in, we use an aidl interface.

As I am new to those interfaces, I wonder if the following would work for the plug-in to return a result to my main application:

I created an interface:

public interface MainAppCallback {
    public void onResult(String result);
}

I implement it in my recording thread:

MainAppCallback callback = new MainAppCallback() {
    @Override
    public void onResult(String result) {
        // Add result to member variable of recording thread
    }
};

In my aidl interface, I defined the methods for the plug-in:

/** Callback to send results to main application
*/
void registerCallback(MainAppCallback callback);

/** The code is documentation enough
*/
void unregisterCallback();

I then want to do something like this in my recording thread:

PlugIn.registerCallback(callback);

Is this the correct way to go, or am I doing something wrong here?


Solution

  • That is correct. Make sure you have defined an aidl file for the callback interface too.

    here is an answer here which already has an example of the 2 aidl files you need to have for callbacks : Android remote service callbacks

    There is also complete sample code here: https://developer.android.com/guide/components/aidl.html