Now we have another day to learn something new about the Service, Ok, let's make something great to learn more about the concurrency API services. In my last article, we understand about Concurrency API of Service and Intent Service and Bound Service. In this article, we will learn about services that use Messenger and AIDL techniques.
Bound Services (Messenger and AIDL)
In this article, I focus on bound services that run in the background in different processes in a single thread and multi-threaded environment. If we are talking about a single thread to send messages in the queue, the first thing that comes to mind is that of Messenger.
Messenger will process messages in different processes using the IPC (Inter Process Communication) remote control technique to communicate with clients and servers. Messenger will be added with the handler, then all the tasks will be in the queue process in a single thread by Handler.
To bind to what service you need to do by passing a Messenger sample reference in the onBind () method and forwarding the handler's reference while creating a Messenger instance. Here the message will be brought in bundle format with a powerful Messenger. Let's look for example to create one service that runs in a different process by using Messenger handled by Handler.
Now let's create an activity class to bind this service to send a message to this server and once again resend this response from server to client using Messenger process by handler.
Here also required the Service Connection to build the connection between the client and the server. The handler will process the response of Messenger and update the user interface. But one more thing does not forget to add this service in AndroidManifest.xml.
Messenger is awesome to communicate with server and client. Let’s check how is AIDL works in different process.
AIDL means the android interface definition language. It can process any task remotely using IPC techniques. Android has a powerful mechanism to communicate between services and clients through IPC techniques. AIDL processes data between remote and secure clients only the things required for clients to be bound by this service. Support AIDL to process generic data using Parcelable. Data processing through Parcelable is fast in IPC mechanism. Different process means I am talking about communication between two different applications.
Let's take a simple word to determine the IPC AIDL mechanism, suppose you consider App1 as a remote server running an AIDL service. What is this service? it gets a list of user information from the data tables and processes to the client or business logic that gets the information from the client and performs some tasks and responds back to the client (Meaning App2). Here the user model should be paired for fast communication.
I think that's a big complex example, Let's give another simple example to explain. I want to multiply by being given two numbers. I will pass two different numbers as input and will notify the AIDL service to return the results in response to me. This is a simple task here but it can require a lot of background tasks for the process, such as downloading audio or video files from the server.
For communication with two different applications or processes, I need to create an AIDL file in both packages of the same application. Why AIDL is required on the client side will also make a proxy copy for remote access.
Let's create two different android studio projects for App1 and App2.
You need to set the build and sync to access the aidl file.
Add the service in AndroidManifest.xml file.
Now in App2, I will try to access the service to get the result. You need to bind via explicit intent service with the client by ServiceConnection.
Here is the xml file.
Wow AIDL is the really awesome feature of service that running in the background in multi-thread environment and its IPC mechanism is very strong to communicate remotely.
Also Read :
- How To Learn Android Room Persistence Library Sqlite Mapper with Kotlin "Android Exmaple + Source Code'
- HTC U11 + GETS unofficial TWRP Recovery, Does not SuPPort Decryption YeT
- HOw To ROOT Android Our Always-UPDATE RooTing Guide FOr Any Phone OR TABLET
- Full Source Code RESKIN To Android 2D Super Red Ball World Support Admob
- List AOSP Android AOSP ROM Oreo Device [UPDATED FREQUENTLY] FUll Download Source Code