AIDL is an acronym for Android Interface Definition language, which is a description language of the Android internal process communication interface, through which we can define communication interfaces between processes.
With Aidl we can complete data communication from the server to the client
In Aidl we can declare any number of methods, except for the built-in types (int boolean, etc.) that need to be imported, with the following rules:
1. The Java primitive type does not need to be imported.
2, String, Lsit, Map, and charsequence do not need to be imported.
Create the Aidl file,new->file-> filename. aidl, add the following code:
Package Com.example.new1;interface Inewservice {void setage (int.); int Getage (); void SetName (String name); String getName ();}
Click Save to refresh the project and the Java code will be generated automatically under Gen.
(The resulting code is sometimes not indented and can be set by right-->source->format)
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/5A/17/wKiom1T116rDRG8AAABsjC3apQw714.jpg "title=" 1.png " alt= "Wkiom1t116rdrg8aaabsjc3apqw714.jpg"/>
In the generated code is another stud class, which he inherits from IBinder. It can be used as the return value of service Onbind, once the service is bound by the services of other programs, the IBinder class instance is sent out, and the method data rewritten in this instance is sent along with it.
Create a new servers, named Newservice.java
Inside declare a stub class, complete the four functions defined above, the code is as follows:
package com.example.new1;import com.example.new1.inewservice.stub;import android.app.service; import android.content.intent;import android.os.bundle;import android.os.ibinder;import Android.os.remoteexception;import android.util.log;import android.widget.toast;public class newservice extends service { private string name= " Www.51ct0.com "; private int age=18; @Override public ibinder onbind (intent arg0) { // TODO Auto-generated method stub toast.maketext (newservice.this, "Onbind", toast.length_long). Show ();     LOG.I ("SERVICE", "Onbind"); return mbinder; //return Interface } public void oncreate () { Super.oncreate ();         LOG.I ("SERVICE", "Oncreat"); } public void onstart (Intent intent,int startid) {        LOG.I ("SERVICE", "OnStart"); } public void ondestroy () { log.i (" SERVICE "," Ondestory "); } private inewservice.stub Mbinder = new stub () { @Override //functions that implement interface definitions public void setage (int age) throws Remoteexception { // todo Auto-generated method stub NewService.this.age = age; } @Override public int Getage () throws RemoteException { // TODO Auto-generated method stub return NewService.this.age; } @Override public void setname ( String name) throws RemoteException { // TODO Auto-generated method stub NewService.this.name=name; } @Override public string getname () throws RemoteException { // TODO Auto-generated method stub return NewService.this.name; } };}
So far, all the functions in the interface have been implemented, and the following will implement the client's call:
Create a new Activity.java with the following code:
package com.example.new1;import android.app.activity;import android.app.actionbar;import android.app.fragment;import android.content.componentname;import android.content.intent;import android.content.serviceconnection;import android.os.bundle;import android.os.ibinder;import android.os.remoteexception;import android.util.log;import android.view.layoutinflater;import android.view.menu;import android.view.menuitem;import android.view.view;import Android.view.view.onclicklistener;import android.view.viewgroup;import android.widget.button;import android.widget.edittext;import android.widget.textview;import android.widget.toast;import android.os.Build;public class MainActivity extends Activity { private textview textview; private inewservice inewservice;// Declaration Interface     PRIVATE&NBSp Serviceconnection conn=new serviceconnection () { @Override public void onserviceconnected ( COMPONENTNAME ARG0, IBINDER ARG1) { // TODO Auto-generated method stub inewservice = inewservice.stub.asinterface (arg1);//access to interface try { inewservice.setname ("I am Activity"); //calling Functions inewservice.setage (+); textview.settExt (Inewservice.getname () +inewservice.getage ()); } catch (remoteexception e) { // TODO Auto-generated catch block e.printstacktrace (); }       LOG.I ("SERVICE", "Success" ); } @Override public void onservicedisconnected (componentname arg0) { // TODO Auto-generated Method stub &nBSP;         LOG.I ("SERVICE", "Errer"); } }; @Override Protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); setcontentview ( R.layout.activity_main); button button1= (Button) This.findviewbyid (R.ID.BTN1); textview= (TextView) This.findviewbyid (R.ID.MYTEXT1); button1.setonclicklistener (new onclicklistener () { @ Override public void onclick ( VIEW ARG0)  {    &Nbsp; // todo auto-generated method stub bindservice (New intent (Mainactivity.this,newservice.class), conn,bind_auto_create); } } ); }}
This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1617053
Android Service Details (iii) AIDL use resolution