ποΈ ππππ ππππππππππ’ ππππππ ππππππ£πππ π°ππππππ πΈππππππππ π³πππππππππ π»πππππππ (π°πΈπ³π») πππ ππππππππ πππππππππππππ πππππππ ππππππππ ππππππππ πππ ππππππ πππππππ ππ π°πΈπ³π» ππ π°ππππππ πππππππππππ.
π²πππππππ |
---|
Demo |
About AIDL |
How communication occurs in the sample |
How to run the sample application |
Demo.of.Client.mp4
AIDL
is calledAndroid Interface Definition Language
.- It acts as an interface between the server and the client.
- Both server and client agree to communicate using this interface.
- In Android normally processes do not communicate with each other.
- If we want the processes to communicate with each other, We need to convert the data to primitives that the operating system can understand and then communicate, It is complex --> The AIDL makes this process simple to achieve.
- Any method defined in your service to be accessed simultaneously by more than one applications.
- If you want to share the data from your application so that other applications access it.
- All the primitive types like
int
,char
,boolean
,long
, and so on... String
CharSequence
List<Supported data type>
Map<Supported data type>
Generic maps
, such asMap<String,Integer>
are not supported.
1
Server application
- In the server application, We shall create an
aidl
file interface with methods exposed where on building the interface the code is generated behind the scenes by Android that takes care of marshaling the data that is used for communication. - Server defines a service class that extends an android
service
class and weOverride
theonBind
method. - In the usual case of normally running the background service, We would return the
IBinder
asnull
but in our case here we will write anIBinder
implementation. - The
IBinder
implementation will have all the abstract methods overridden and defined where we define the server implementations.
2
Client application
- In the client also define the same
aidl
file as a server(You can even copy-paste it). - Now important to note is the package hierarchy must be the same here as the server else it won't work.
- Build the project so that the Android generates code behind the scenes.
- Define a service connection implementation
- Here there are two methods
onServiceConnected
andonServiceDisconnected
methods overridden. - At
onServiceConnected
method we get the reference tointerface reference
which requiresIBinder
as input to it and the reference is kept globally. - Using this reference we can access and communicate with the server.
- Here there are two methods
3
Where we define the aidl files
Server |
Client |
---|---|
4
How client initiates communication with server
- We prepare an intent with inputs
- It has an
Intent filter name
- The
package name
of the server - The
component name
that contains the service name in the server.
- It has an
- We call the
bindService
that hasintent
,service connection
, and aflag
to initiate the connection. - The earlier kept global reference is used to call the interface methods which in turn communicates with the server and gets the result from the server.
- There are two applications in the repository. One is for
server
and another forclient
- Server consists of a
service
and does not contain any activity, client contains anactivity
that contains a button to initiate data retrieval from the server application. - Install the server App and install the client app also.
- Now click the specific buttons on the client to retrieve specific data from the server.
πΈπ π’ππ ππππ ππππ πππππππ ππ π ππππππ πππ ππ’ πππππππ, πΈ π ππππ πππππππ’ ππππππππππ ππ.
ππππ ππππππππππππ ππππππππππ πππ ππππ πππππππππππ πππππππππ ππππππππππππ.
π΅ππππππ ππππππππ πππ πππ ππ’π π ππππππ, π΅πππ ππ πππππ ππππ.
πππππππ ππ ππ’ ππππππππ πππ β ππππππ ππ πππ πππππ πππππ ππ ππππ ππππ. βοΈ
ππππ πππππππ ππ ππππππππ πππππ πππ π°πππππ π»ππππππ πΈ.πΆ - πππ πππ π»πΈπ²π΄π½ππ΄ ππππ πππ πππππππ.