In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to use AIDL in Android? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
Usage of Android AIDL I. what is an AIDL service
Services that are generally created cannot be accessed by other applications. In order to enable other applications to access the services provided by this application, the Android system uses remote procedure call (Remote Procedure Call,RPC) to implement. Like many other RPC-based solutions, Android uses an interface definition language (Interface Definition Language,IDL) to expose the interfaces of services. Therefore, this service that can be accessed across processes can be called an AIDL (Android Interface Definition Language) service.
II. Basic grammar of AIDL
AIDL uses simple syntax to declare an interface, describing its method and its parameters and return values. These parameters and return values can be any type, even other AIDL-generated interfaces.
For the basic data types of the Java programming language (int, long, char, boolean,String,CharSequence) collection interface types List and Map, no import statement is required.
If you need to use other AIDL interface types in AIDL, you need import, even under the same package structure. AIDL allows you to pass classes that implement the Parcelable interface, requiring import
It should be noted that for non-basic data types, which are not String and CharSequence types, direction instructions are required, including in, out and inout,in are set by the client, out is set by the server, and inout can be set for both.
AIDL only supports interface methods and cannot expose static variables.
Third, the android application layer uses AIDL3.1, step 1, create a file with the extension A.aidl in the Java package directory of the Eclipse Android project, and write down the required interfaces. If the contents of the aidl file are correct, ADT automatically generates an A.Java interface file in the gen directory.
2. Create a service class (a subclass of Service). And create an inner class in the created service class to implement the Java interface generated by the aidl file.
3. When the onBind method of the service class returns, the inner class object that implements the aidl interface is returned.
4. Configure the AIDL service in the AndroidManifest.xml file. In particular, it is important to note that the attribute value of android:name in the tag is the ID that the client wants to reference the service, that is, the parameter value of the Intent class. 3.2.1.Create the file IMyService.aidl:
Contents of the file:
Package du.pack
Interface IMyService {
/ / there is only one interface
String getValue ()
}
3.2.2, create service classes and implement inner classes
Public class MyService extends Service {
@ Override
Public IBinder onBind (Intent arg0) {
/ / return the objects of the inner class to the client for use
Return new MyServiceImpl ()
}
/ / create an inner class that inherits from IMyService.Stub
Public class MyServiceImpl extends IMyService.Stub {
/ / the interface in the AIDL file must be implemented
Public String getValue () throws RemoteException {
Return null
}
}
}
Note that in the service we wrote, the onBind method must return an object instance of the MyServiceImpl class, otherwise the client cannot get the service object. 3.2.3. Configure MyService classes in the AndroidManifest.xml file
The "du.pack.IMyService" above is the ID that the client uses to access the AIDL service.
Create a new Eclipse Android project, and copy the IMyService.java file under the gen directory automatically generated by the remote server together with the package directory to the src directory of the client project.
4.2.To invoke an AIDL service, you must bind the service before you can get the service object.
Public class AidlClientTestActivity extends Activity {
/ / objects on the remote server
IMyService mIMyService
Private ServiceConnection mConnection = new ServiceConnection () {
Public void onServiceConnected (ComponentName name, IBinder service) {
/ / bind successfully, get the object on the remote server, and the target is complete!
MIMyService = IMyService.Stub.asInterface (service)
}
Public void onServiceDisconnected (ComponentName name) {
/ / unbind
MIMyService = null
}
}
@ Override
Public void onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
SetContentView (R.layout.main)
/ / bind remote server service
Intent serviceIntent = new Intent ("du.pack.IMyService")
BindService (serviceIntent, mConnection, Context.BIND_AUTO_CREATE)
}
}
5. Usage summary: review the whole process of calling:
Server side: abstract the interface that needs to be open to the aidl file, then implement the interface in your own inner class, and return the inner class object to the client when you are bound. On the client side: when we need to remotely a Service, we go to bindService as if we were binding a local Service, and then get an Ibinder object (such as Service) in the successfully bound callback function (that is, the onServiceConnected method). When we call a statement such as IMyService.Stub.asInterface (service), we can get an open interface interface object on the server side, and the client can call the method of this object directly. It is like calling a remote Service object directly.
4. Use AIDL in Framework
Using AIDL in Framework, we analyze it through the SystemService of ITelephonyRegistry. The main function of this service is to monitor the events related to calls. We focus on the implementation structure of AIDL and do not pay attention to the specific implementation of ITelephonyRegistry.
1. Related to AIDL files
Let's first take a look at the AIDL file for this service:
@ ITelephonyRegistry.aidl
Interface ITelephonyRegistry {
Void listen (String pkg, IPhoneStateListener callback, int events, boolean notifyNow)
Void notifyCallState (int state, String incomingNumber)
Void notifyServiceState (in ServiceState state)
Void notifySignalStrength (in SignalStrength signalStrength)
Void notifyMessageWaitingChanged (boolean mwi)
}
Let's take a look at the real implementation of this service:
@ TelephonyRegistry.java
Class TelephonyRegistry extends ITelephonyRegistry.Stub {
TelephonyRegistry (Context context) {
.
}
Void listen (String pkg, IPhoneStateListener callback, int events, boolean notifyNow) {
.
}
Void notifyCallState (int state, String incomingNumber) {
.
}
Void notifyServiceState (in ServiceState state) {
.
}
Void notifySignalStrength (in SignalStrength signalStrength) {
.
}
Void notifyMessageWaitingChanged (boolean mwi) {
.
}
}
The above two files are the core of the service, the aidl file specifies the function of the service, and the java file is the concrete implementation of the function. However, TelephonyRegistry does not inherit Service's class at this time, that is, he is not currently qualified as a Service. So how did he become a service?
2. Registration process of the service
The answer can be found in SystemService.
@ SystemServer.java
Class ServerThread extends Thread {
@ Override
Public void run () {
Try {
TelephonyRegistry = new TelephonyRegistry (context)
ServiceManager.addService ("telephony.registry", telephonyRegistry)
}
}
}
We see that in this step, the telephonyRegistry object (that is, a subclass object of ITelephonyRegistry.Stub) is registered with ServiceManager as a Service. And the registered name is "telephony.registry"
With this step, TelephonyRegistry can be opened to the client as a service provider. In other words, with this step, TelephonyRegistry can be counted as a real Service and can accept applications for connections from clients.
So next, how do we get this Service?
3. How to get registered services
Now that the service is registered through ServiceManager, we need to get its service object again through ServiceManager.
Private ITelephonyRegistry sRegistry
SRegistry = ITelephonyRegistry.Stub.asInterface (ServiceManager.getService ("telephony.registry"))
In this way, we get the object sRegistry of ITelephonyRegistry.aidl.
This is the answer to the question about how to use AIDL in Android. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.