Completely clear aidl and its use in Android

Source: Internet
Author: User

1, why should there be aidl?No matter what you learn, the first thing is to make it clear why you have it. Do not say that existence is justified. Existence is certainly reasonable, but you are still not clear.

For Aidl, there are some simple notions that aidl can access other applications across processes and communicate with other applications, and I'll tell you. Very many techniques can be visited, such as broadcast (application A in Androidmanifest.xml to specify the action of the broadcast) application B sends the specified action broadcast, a can receive information, it can also be considered as a communication between different applications (but such communication is one-way). Also, as contentprovider, exposing data to other applications via URI interfaces is not a communication between applications.

Perhaps the most confusing thing is that Android comes with Messager, which is the communication between apps. Then why should there be aidl? Official document Introduction Aidl in a sentence:

Note:using Aidl is necessary only if you allow clients from different applications to access your service for IPC and WAN T to handle multithreading in your service. If you don't need to perform concurrent IPC across different applications, you should create your interface by Implementi ng a Binder or, if you want to perform IPC, but does not need to handle multithreading, implement your interface using a Mes Senger. Regardless, be sure this understand Bound Services before implementing an aidl.
The first sentence is the most important, "You only have to use aidl when you agree to have access to your service from a different client and need to deal with multithreading issues." In other cases you will be able to choose other methods. If you use Messager, you can also communicate across processes.

Visible Aidl is a multi-threaded, multiple-client concurrent interview. Messager is a single-threaded process. Or the official document is clear, a word can understand why to have aidl. So write a aidl and try it.

2, Aidl useFirst, define the Aidl file
Iremoteservice.aidlpackage com.example.android;//Declare Any non-default types here with import statements/** example  Service Interface */interface Iremoteservice {    /** Request the process ID of the This service, to does evil things with it. *    /int getpid ();    /** demonstrates some basic types that's can use as parameters     * and return values in Aidl.     *    /void basictypes (int anint, long along, Boolean aboolean, float afloat,            double adouble, String astring);}
This code is also an official document.

Name is Iremoteservice.aidl. Put under the Com.example.android package (this can be arbitrary), after saving the Android compiler will be in the Gen folder to actively generate Iremoteservice.java files

Second, define our service, Ddservice.java, and need to register in Androidmanifest.xml, and add "duanqing.test.aidl" Action
Package Com.example.service;import Com.example.android.iremoteservice;import Android.app.service;import Android.content.intent;import Android.os.ibinder;import Android.os.process;public class DDService extends Service {@ overridepublic void OnCreate () {super.oncreate (); System.out.println ("Ddservice onCreate ..." + "Thread:" + thread.currentthread (). GetName ());} @Overridepublic ibinder onbind (Intent arg0) {System.out.println ("Ddservice onbind"); return mbinder;} Private final Iremoteservice.stub Mbinder = new Iremoteservice.stub () {public int getpid () {System.out.println ("Thr    EAD: "+ thread.currentthread (). GetName ());        System.out.println ("Ddservice getpid");    return Process.mypid (); } public void Basictypes (int anint, long along, Boolean aboolean, float afloat, double adouble, String astring)    {System.out.println ("Thread:" + thread.currentthread (). GetName ()); System.out.println ("Basictypes adouble:" + adouble + "Anint:" + anint+ "Aboolean" + Aboolean+ "astring" + astring); }};}

So our service end is finished. Execute the server to the emulator (or the phone). Wait a moment to see the information printing, focusing on "thread name"
Third, the implementation of the client test code Create a new project with the same need to join the AIDL protocol file (this is a standard protocol file.) Define external services), here I have listed my test code:
Package Com.example.aidlclient;import Android.app.activity;import Android.content.componentname;import Android.content.context;import Android.content.intent;import Android.content.serviceconnection;import Android.os.bundle;import Android.os.ibinder;import Android.os.process;import Android.os.remoteexception;import Android.view.view;import Com.example.android.iremoteservice;public class Mainactivity extends Activity {private    Iremoteservice Remoteservice;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.activity_main); } serviceconnection conn = new Serviceconnection () {@Overridepublic void onservicedisconnected (componentname name) {} @Overridepublic void onserviceconnected (componentname name, IBinder service) {Remoteservice = IRemoteService.Stub.asInterface (service); try {int pid = remoteservice.getpid (); int currentpid = Process.mypid (); System.out.println ("Currentpid:" + Currentpid + "remOtepid: "+ pid"); Remoteservice.basictypes (1223, True, 12.2f, 12.3, "Our love, I am Clear");} catch (RemoteException e) {e.printstacktrace ();}    System.out.println ("Bind success!" + remoteservice.tostring ());}; /** * Monitor button click * @param view */public void ButtonClick (view view) {System.out.println ("Begin Bindservice    ");    Intent Intent = new Intent ("Duanqing.test.aidl");    Bindservice (Intent, Conn, context.bind_auto_create);    } @Override protected void OnDestroy () {Super.ondestroy ();    Unbindservice (conn); }}
4. OperationClick Clientbutton, run. See information print:
look at the server print, Ddservice onCreate ... Thread:main, the main thread, when the client invokes the server-side Getpid method, the server is running in Thread:binder2, and when the client invokes the server-side Basictype method, the server runs in Thread:binder1

Completely clear aidl and its use in Android

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.