Android binder inter-process communication mechanism 5-Service Registration and proxy object acquisition, android5-service

Source: Internet
Author: User

Android binder inter-process communication mechanism 5-Service Registration and proxy object acquisition, android5-service

ServiceManager is actually a Service, but its Server implementation does not use the structure of the Binder library, but directly open binder/dev for communication, not universal.

The following uses MediaPlayService in MediaServer as an example to describe how to use the Service of the binder communication mechanism more commonly.

1. Start Service Components

The Service component runs in the Server process. The Server process is usually started from its main function and usually does two things at startup:

1. register the Service components in it to ServiceManager.

2. Start a Binder thread pool to wait for and process Client process communication requests.

Take MediaServer and its MediaPlayService as an example. The entry location is in frameworks \ av \ media \ mediaserver \ Main_mediaserver.cpp.

The structure of MeidaPlayService is as follows:

  

The complete structure is fully implemented according to the structure of the binder library.

The startup process is as follows:

  

In the talkWithDriver call, when ioctl is used to interact with the binder driver, the communication data is encapsulated into a binder_write_read struct and communicated using the BINDER_WRITE_READ protocol.

In the Binder driver, binder_ioctl is used to distribute the data to be processed. Finally, the ServiceManager is notified of the registration request through binder_thread_read and binder_thread_write, and the processing result is returned to the MediaServer process through the Binder driver again.

2. Get the Service proxy object

The Service proxy object acquisition process is a Client process and ServiceManager process standard Binder communication process:

As follows:

  

Specifically, getService calls checkService, and IServiceManager's real implementation class is BpServiceManager, which is the proxy object of ServiceManager. The Client process communicates with the ServiceManager process through the Binder driver.

 


How does android make two apks communicate with each other?

A.apkand B .apkare all my own. Now I want to download a Control ID of a.apk in B .apkand send a key event to it. How to implement this? Thank you! That is, use B to control.

These two apks are in two processes (or can be configured as a process. However, I have not tried to directly perform this operation .)

Therefore, there are two methods:
1. RPC
2. BroadcastRecever or Service

The second method is simpler.
For example, if B controls,
A defines the Service, such as com. my. ServerA. Custom actionFilter "com. my. serverA. action"
Call Intent it = newIntent ("com. my. serverA. action") in B ");
It. putExtra (...); // pass your custom control commands.
B. startService (it );
In A, the onStartService (Intent it) of ServerA ). Parse the control command and perform corresponding operations.

The same applies to BroadcastRecever.

Aidl, one of the IPC methods of the android platform, is based on the system Ibinder mechanism.
Most examples on the Internet use an apk to test and call the service. Now I will call it under two projects.
One is the server project and the other is the client project.
First, we create a server project. here we need to implement the aidl file and a service. activity is only used to start the service. Of course, you can also start the service by sending a broadcast.
First, check the IAidlService. aidl file:

Java code
Package com. ds. server;
Interface IAidlService {
Int getType ();
}

In this way, the IAidlService. java file will be generated under gen during automatic compilation in eclipse (the client project should be used under the lamp ).
Create a new service, which contains the has a IAidlService's stub object. The service Code is as follows:

Java code
Package com. ds. server;

Import android. app. Service;
Import android. content. Intent;
Import android. OS. IBinder;
Import android. OS. RemoteException;
Import android. util. Log;

Public class AidlService extends Service {

Private IAidlService. Stub mBinder = new IAidlService. Stub (){

@ Override
Public int getType () throws RemoteException {
// TODO Auto-generated method stub
Return 5;
}
};

Private void Log (String str ){
... The remaining full text>

Recently, I am looking for android development. I have summarized some questions that I still don't know. I hope you can answer them one by one.

1. Write a service and the application uses createservice
2. The runtime is generally based on uid, and the file system has the same permissions as linux.
3. You can know the class name or how to write the specific manifest.
4 loose is a thread that processes the message in message queue cyclically, while handler is a class for the methods related to message sending.
5. pipe is not clear about the binder Mechanism. The socket uses sockets to send content for ipc. The binder uses shared memory and servicemanager as the service manager.
6 surfaceflinger manages windows,
7. bionic is a simplified version of glic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.