Question

google docs have a tutorial on how to make an AIDL service. However, when I tried to do the same, I got an error "The method registerCallback(IRemoteInterface) is undefined for the type IRemoteInterface.

I have not seen this "registerCallback" method in any tutorial that I have been googling and my question is why is this not working/why do other places not use it?

I feel like part of my issue is a fundamental misunderstanding about services and their 'callback' to send information to what it is bound to it. Thanks!

AIDL:

package com.mine.ben;

import com.mine.servicenexus.RelPoint;


interface IRemoteInterface {

    Location getLastLocation();
    RelPoint getRelPoint();
    int logControlActivity(in String text,in int severity);
    int getRunningStatus();
}

Updated question: I get a syntax error in my AIDL file when i add

void registerCallback(IRemoteServiceCallback);
void unregisterCallback(IRemoteServiceCallback);

I have cleaned my workspace and it builds automatically. Is this a problem with the gen file?

Was it helpful?

Solution

Few lines from my application:

interface IGpsService {
    Bundle getNavigationMessage();
    void resetStatistics();
    void recordingEnable(boolean recordingEnabled, boolean continueLastTrack);
    void registerCallback(IGpsChanged cb);
    void unregisterCallback(IGpsChanged cb);
    boolean isRecordingEnabled();
    void setGhost(int trackId);
    void startGhost();
    void sendUserEvent(int eventId);
}

second file for calback interface itself:

interface IGpsChanged{
oneway void onLocationChanged(out Bundle message);
}

Best of all - usually you do not need those .aidl definitions. This is necessary only when exposing service methods outside of the application.

It's also helpful to understand how it works - forst aidl defines basic service's methods. registerCallback is not a "special" method, it's as oridinary as any other, just enables defining callbacks for two-way communication (in this particular case - to send some position info from sesrvice to binded activity (or other component).

As you mentioned in comment - you do not want to create service, just consume some service from outside of application. In that case you need those external aidl file, not the one written by you. Stubs for consuming service's method will be generated in the /gen directory.

OTHER TIPS

google docs have a tutorial on how to make an AIDL service.

That is not a tutorial. It is just ordinary documentation.

However, when I tried to do the same, I got an error "The method registerCallback(IRemoteInterface) is undefined for the type IRemoteInterface.

That is because you do not have a method named registerCallback() in your AIDL.

I have not seen this "registerCallback" method in any tutorial

It is not in a tutorial. The only occurrences of registerCallback() in the Web page that you linked to are from "some sample code demonstrating calling an AIDL-created service, taken from the Remote Service sample in the ApiDemos project". The ApiDemos project is in your SDK installation, if you elected to download sample code from the SDK Manager.

Code example from AIDL guide that you have referenced is taken from the Remote Service sample in the ApiDemos project. And registerCallback() in it is implemented by using android.os.RemoteCallbackList<E extends android.os.IInterface> object.

Make an .aidl file like:

package com.example;

oneway interface IRemoteServiceCallback {
    /**
     * Goes to client.
     */
    void valueChanged(int value);
}

Make another .aidl file where you import this interface and use it as a method's parameter like:

package com.example;

import com.example.IRemoteServiceCallback;

interface IRemoteService {
    /**
     * Goes to service.
     */
    void registerCallback(IRemoteServiceCallback cb);
}
  • Generate code from .aidl on both sides.
  • Implement IRemoteService.Stub in service and return it in onBind()
  • Implement IRemoteServiceCallback.Stub in client and pass it in ServiceConnection's onServiceConnected() callback to the received from IRemoteService.Stub.asInterface() instance of IRemoteService.

Now you service can talk back to the client over the passed IRemoteServiceCallback implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top