Question 1:
I am studying Android service and often see code like this:
private ISampleService.Stub sampleServiceIf = new ISampleService.Stub(){}
What is .Stub
?
Question 2:
I checked "AIDL", but I want to know why we have to use that instead of the Java interface file?
'Stub' is a class that implements the remote interface in a way that you can use it as if it were a local one. It handles data marashalling/unmarshalling and sending/receiving to/from the remote service.
The Android Interface Definition Language (AIDL) is similar to other IDLs you might have worked with. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other using interprocess communication (IPC).
The stub file contains C code that provides the glue that holds the Java class and its parallel C structure together. To generate a stub file use javah 's -stubs option. Again remember to run javah on the Java class. By default, javah will place the resulting stub file in the same directory as the . class file.
An AIDL file is used by Android app developers to enable communication between different apps. It contains Java source code that defines an interface, or contract, for how apps can communicate with each other. AIDL is an implementation of the Interprocess Communication (IPC) protocol provided by Android.
'Stub' is a class that implements the remote interface in a way that you can use it as if it were a local one. It handles data marashalling/unmarshalling and sending/receiving to/from the remote service. The term 'stub' is generally used to describe this functionality in other RPC methods (COM, Java remoting, etc.), but it can mean slightly different things.
The IDL (Interface Definition Language) is generally language independent, and you could theoretically generate C++ or Python stub code from it. The Android one is Java-based though, so the distinction is subtle. One difference is that you can only have a single interface in an .aidl file, while Java allows multiple classes/interfaces per .java file. There are also some rules for which types are supported, so it is not exactly the same as a Java interface, and you cannot use one instead of AIDL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With