Android in Aidl Android studio built in Aidl

Source: Internet
Author: User
Tags gettext log e

1.aidl:android Interface Definition Language, is a description language of the Android internal process communication interface, through which we can define the communication interface between processes
Icp:interprocess communication: Internal process communication

2. Since aidl can define and implement process communication, how do we use it? The steps are described in detail in document/android-sdk/docs/guide/developing/tools/aidl.html:

--1.create your. Aidl file-this file defines an interface (YOURINTERFACE.AIDL) that defines the methods and fields avail Able to a client.
To create your aidl file, I give an example of the Aidl file, which is defined as follows: It is similar to Java code, but it is important to note that it can refer to interfaces defined in other AIDL files, but cannot refer to interfaces defined in your Java class file.

[Java]View PlainCopy
    1. Package com.cao.android.demos.binder.aidl;
    2. Import com.cao.android.demos.binder.aidl.AIDLActivity;
    3. Interface Aidlservice {
    4. void Registertestcall (aidlactivity CB);
    5. void Invokcallback ();
    6. }

Android Studio built in Aidl

Create a new folder in the main directory, named Aidl, and a new package in that directory, the package name is the same as the one in Androidmanifest, and then the Aidl file is created under the package, and after the Build/generated/source Auto-generated Java files can be seen under/aidl/debug

Zhuo Learning Teng
Links: http://www.zhihu.com/question/21581761/answer/45895797
Source: Know
Copyright belongs to the author.     Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source. AIDL Inter-process communication in Android

About IPC should not be introduced more, the Android system between the process can not share memory, then if two different applications need communication between the what to do? For example, a company project to be updated, product requirements are attached to the current project to develop a plug-in, but this plug-in function and interface is more complex, not with the current project in a process, but also to use the current project has been written in some of the things, Because the newly developed plug-in and current project that is attached to the current project is not a process, and therefore cannot share memory, there is a problem, so there is a need to provide some mechanism for data communication between different processes, this mechanism is aidl.

One simple example of Aidl in Android

If so, now there is a project to provide a more mature method of calculation, and now I want to develop a software one of the modules want to use a computing class, and I do not want to re-write, then can be achieved through the aidl. Suppose that the program that has been developed to provide a more mature computing class is called aidlcalculatedemoserver (equivalent to a server), and the program I'm writing is called aidlcalculatedemoclient (the equivalent of a client), Similar to the client server model. First, to the close look at the engineering structure diagram:

Figure 1-1 Server Figure 1-2 Client

Now assume that the program you write to invoke the service side of the interface, input NUM1 and num2, remote operation, call the server interface, the service side of the operation, and then return the results to the client, as follows:

Then take a look at the implementation, the first need to define the Aidl interface, both the client and server are defined, and in the same package, that is, figure 1-1 and figure 1-2 Com.example.aidl.calculate in the Calculateinterface, where the code is as follows:

1 package com.example.aidl.calculate;2 3 interface Calculateinterface {4     double docalculate (double A, double b); 5}

Compilation discovery, directory structure 1-1 and figure 1-2 Gen/com.example.aidl.calculate in the Calculateinterface.java file, the contents are as follows:

1 package com.example.aidl.calculate;2 3 interface Calculateinterface {4     double docalculate (double A, double b); 5}

Define the interface is to look at the server and the client's code, where the server mainly look at the Calculateservice code, this one inherits the service class, in which the aidl in the interface to give practical significance, as follows:

 1 package com.example.calculate; 2 3 Import Com.example.aidl.calculate.CalculateInterface; 4 Import com.example.aidl.calculate.CalculateInterface.Stub; 5 6 Import Android.app.Service; 7 Import android.content.Intent; 8 Import Android.os.IBinder;     9 Import android.os.remoteexception;10 Import android.util.log;11 public class Calculateservice extends Service {13 The private static final String TAG = "Calculateservice"; 15 1 6 @Override17 public IBinder onbind (Intent arg0) {mon/TODO auto-generated method Stub19 LogE ("on Bind () ") return mbinder;21}22 @Override24 public void OnCreate () {//TODO auto-g  Enerated method Stub26 LogE ("OnCreate ()"); Super.oncreate ();}29 @Override31 Public         void OnStart (Intent Intent, int startid) {+//TODO auto-generated method stub33 LogE ("OnStart ()"); 34 Super.onstart(Intent, Startid);}36 @Override38 public boolean onunbind (Intent Intent) {//TODO Auto-ge Nerated method Stub40 LogE ("Onunbind ()"); return Super.onunbind (intent);}43 @Override4 5 public void OnDestroy () {//TODO auto-generated method stub47 LogE ("OnDestroy ()"); R.ondestroy ();}50 the private static void LogE (String str) {LOG.E (TAG, "--------" + str + "------         --");}54, final calculateinterface.stub mbinder = new Calculateinterface.stub () {56 57 @Override58 public Double docalculate (double A, double b) throws RemoteException {//TODO Aut             O-generated method Stub60 log.e ("Calculate", "Remote Computing"); Calculate Calculate = new Calculate (); 62 Double answer = Calculate.calculatesum (A, b); return answer;64}65};66}

Then you can see that the key service is complete, then how to access the client, to bind the service and a serviceconnection class to complete, as follows:

 1 package com.example.calculate; 2 3 Import android.app.Activity; 4 Import Android.content.ComponentName; 5 Import Android.content.Context; 6 Import android.content.Intent; 7 Import android.content.ServiceConnection; 8 Import Android.graphics.Color; 9 Import android.os.bundle;10 Import android.os.ibinder;11 import android.os.remoteexception;12 Import ANDROID.UTIL.LOG;13 Import android.view.view;14 import android.widget.button;15 import android.widget.edittext;16 Import android.widget.textview;17 Import com.example.aidl.calculate.calculateinterface;19 Import COM.EXAMPLE.AIDLCALCULATEDEMOCLIENT.R;20 public class Calculateclient extends Activity {$ private static final STR                 ing TAG = "calculateclient";                 btncalculate;25 EditText etnum1;27-Private    EditText etnum2;29-Private          TextView tvresult;31 Calculateinterface mservice;33 34 Private Serviceconnection mserviceconnection = new Serviceconnection () {@Ove              rride37 public void onservicedisconnected (componentname name) {//TODO auto-generated method stub39         LogE ("Disconnect service"), Mservice = null;41}42 @Override44 public void onserviceconnected (componentname name, IBinder service) {//TODO auto-generated method Stub4 6 LogE ("Connect service"), Mservice = CalculateInterface.Stub.asInterface (service); 48}4 9};50 @Override52 protected void onCreate (Bundle savedinstancestate) {//TODO auto-generate         D method stub54 super.oncreate (savedinstancestate); Setcontentview (R.layout.main); 56 57 Bundle args = new bundle(); Intent Intent = new Intent ("Com.example.calculate.CalculateService"); Intent.putextras (args); 60 Bindservice (Intent, mserviceconnection, context.bind_auto_create); etNum1 = (EditText) findview Byid (r.id.et_num_one); etNum2 = (EditText) Findviewbyid (r.id.et_num_two); Tvresult = (TEXTV         Iew) Findviewbyid (r.id.tv_result); btncalculate = (Button) Findviewbyid (r.id.btn_cal); 68 69 Btncalculate.setonclicklistener (New View.onclicklistener () {@Override72 P ublic void OnClick (View v) {//TODO auto-generated method stub74-Log E ("Start remote operation"); NUM1 = Double.parsedouble (Etnum1.gettext (). toString ()); 7  8 Double num2 = double.parsedouble (Etnum2.gettext (). toString ()), and the String answer = "Calculation result:" + Mservice.docaLculate (NUM1, num2); Tvresult.settextcolor (Color.Blue); Bayi Tvresult.settext (answe         R); RemoteException e) {84}85}86     });}88 LogE (String str) {LOG.E (TAG, "--------" + str + "--------"); 91 }92}

In this way, the merit has been basically gaocheng, finally, we are looking at the server configuration file:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.example.aidlcaculatedemoserver "android:versioncode=" 1 "android:versionname=" 1.0 "> & LT;USES-SDK android:minsdkversion= "8" android:targetsdkversion= "/> <application android: Allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:them            E= "@style/apptheme" > <activity android:name= "com.example.aidlcaculatedemoserver.MainActivity" Android:label= "@string/app_name" > <intent-filter> <action android:name= "and            Roid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> <service android:name= "Com.example.calculate.CalculateService"  > <intent-filter>              <action android:name= "Com.example.calculate.CalculateService"/> </intent-filter> </service> </application></manifest>

Android in Aidl Android studio built in Aidl

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.