The role of Aidl in the Android system

Source: Internet
Author: User

aidl,android Interface Definition language abbreviation, it is an Android internal process communication Interface Description Language, through which we can define the communication interface between processes. Recently looked at the use of Aidl in the Android system, on the Internet to see a lot of novice friends do not understand the actual role of Aidl, Android provides a lot of interprocess communication components, such as action, broadcast, Contentprovide can achieve inter-process communication, why use aidl this thing? I have implemented a aidl example of myself in the Android source code to explain briefly the role of Aidl.

have developed Bluetooth or WIFI app friends must know, to operate it must first obtain a management class, such as WIFI management class is Wifimanager, through Getsystemservice (Context.wifi_service) will be able to get the management privileges of WiFi, this provides a lot of methods can let the user to operate it, such as open WiFi can call setwifienabled (True) method. So what exactly did the manager do? How is it possible to turn on WiFi? In fact, this manager is only a management class, the real work of the other people, is a call Wifiservice system services. In the Android system there are a lot of Manager,wifi management class called Wifimanager, Bluetooth management class called Bluetoothmanager, but, As long as there is Xxxmanager.java, there will be ixxx.aidl, and there are xxxservice.java. This Aidl class is a bridge for manager and service communication.

Let's look at an example of my add:

First in the Android source code in the frameworks/base/core/java/android/os/directory to add a imytestservice.aidl, the general system Aidl files are placed in this directory.

Package android.os;/** {@hide} */interface imytestservice{    void Open ();    void close ();}

About the language specification of Aidl I'm not going to say much, actually it's almost like a Java write interface (it's an interface language). There are only two simple methods defined, open and close.

Then add a sentence in the frameworks/base/android.mk: Core/java/android/os/imytestservice.aidl. The target of the Android system is to be specified by Android.mk, and a custom Aidl file system is added here to compile the file, resulting in a file called Imytestservice.java. In this section, it is very intuitive to see that eclipse automatically compiles this aidl after the project has added the Aidl file, and the resulting files are stored in the Gen directory.

After the aidl has been added, add a Mytestservice.java in the frameworks/base/services/java/com/android/server/directory and inherit Imytestservice this aidl.

Package Com.android.server;import Android.net.wifi.wifimanager;import Android.content.context;import Android.os.imytestservice;import Android.util.log;public class Mytestservice extends Imytestservice.stub {private STA    Tic final String TAG = "Mytestservice";    Private Context Mcontext;    Private Wifimanager Mwifimanager;        Public Mytestservice (Context Context/*,windowmanagerservice wm*/) {super ();    Mwifimanager = (Wifimanager) context.getsystemservice (Context.wifi_service);    }/* Close WiFi */public void Close () {mwifimanager.setwifienabled (false);    }/* Open WiFi */public void Open () {mwifimanager.setwifienabled (true); }}

Implementing the Open and close methods defined in the Aidl file, I am very simple here, open is to turn on the wifi,close to turn off WiFi, of course, you can also implement your custom functions, this is unlimited.

Then add a static string to the Frameworks/base/core/java/android/content/context.java file: public static final String my_test_service = " My_test_service ";

Finally add this service to the system service and add the following code to the thread's Run method in Frameworks/base/services/java/com/android/server/systemserver.java:


try {slog.i (TAG, "My Test Service");                 MyService = new Mytestservice (context);             Servicemanager.addservice (Context.my_test_service, MyService);             } catch (Throwable e) {reportwtf ("Starting my Test Service", e); }

MyService need to be declared in front.

Systemservice is the Android system will be called after running, the meaning is to add mytestservice to the system service, and take the name Context.my_test_service, that is My_test_ Service Does this look familiar to you? Each developer will certainly invoke system services during the development process, such as power Management Services: Getsystemservice (Context.power_service), which is also added here. Android has a lot of system services, here is not one by one cases, interested friends can see this file on their own.


Now that the aidl has been added, the service has been added, and added to the system service, there is one less manager that can be called by the third-party program.

Add the Mytestmanager.java file under the frameworks/base/core/java/android/device/directory:


Package Android.device;import Android.util.log;import Android.content.context;import android.os.RemoteException; Import Android.os.imytestservice;import Android.os.servicemanager;public class Mytestmanager {private static final String TAG = "Mytestmanager";
Private Imytestservice Mtestservice; Public Mytestmanager () {Imytestservice mservice = imytestservice.stub. Asinterface (SERVICEMANAGER.G        Etservice (Context.my_test_service));    Mtestservice = Mservice;        }/** * Return True if open succeed * @see #open my Function () */public Boolean openscanner () {        try {mtestservice.open ();        } catch (Android.os.RemoteException e) {return false;    } return true;         }/** * Return true if close succeed * @see #close my Function () */public Boolean closescanner () {         try {mtestservice.close ();         } catch (Android.os.RemoteException e) {return false;    } return true; }}

This manager uses Aidl stub to get the system level service that was just added, and then call the service method here to operate the service, which is the aidl of the function.

Then we can write a third-party program, get a Mytestmanager, call this management class's Openscanner and Closescanner methods to implement their own functions defined in the service.

Androidmanifest must add <uses-permission android:name= "Android.permission.UPDATE_APP_OPS_STATS"/> permissions, otherwise error, Did not study this permission to play what role, has the understanding friend to share.

Import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.app.activity;import Android.device.mytestmanager;public class MainActivity    Extends Activity {Private Button open, close;    Private Mytestmanager Mtestmanager;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Mtestmanager = new Mytestmanager ();        Open = (Button) Findviewbyid (R.ID.BTN1);        Open.settext ("open wifi");        Close = (Button) Findviewbyid (R.ID.BTN2);        Close.settext ("Turn off WiFi");                Open.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {            TODO auto-generated Method Stub Mtestmanager.openscanner ();        }       }); Close.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//TODO auto-generated Method Stub Mtestmanager.closescanner ();    }        }); }}

This also realizes the Android Kua process communication.

Here is just a very simple example, in order not to understand this piece of friend glimpse, in fact, aidl and system-level services with the use of much less simple, but the general principle is such, such as Android Web services, Google defines a lot of state, Through the background service constantly monitoring the changes in these states to control the network, such as power management, control the brightness of the screen and so on, the complex is a large variety of state changes.

The article is relatively superficial, I hope you have a lot of advice.


The role of Aidl in the Android system

Related Article

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.