Introduced:
Aidl is Android Interface Definition Language
Use:
1. Create a new. aidl file
1 //the package where the Aidl file is located2 PackageCom.houny.demo_aidl.aidl;3 4 //the interface name must match the Aidl file name5 Interfaceisay{6 BooleanSay ();7 BooleanSayint (inti);8 Booleansaystring (String str);9}
2. Create a new service and register it in Mainfirst.xml
1 Public classBackgroundserviceextendsservice{2 3 /**4 * Implement the interface defined in Aidl in this way5 */6Isay.stub say =Newisay.stub () {7 8 //ways to implement an interface9 // ...Ten One }; A - - @Override the Publicibinder onbind (Intent Intent) { - //get that interface object back out . - returnsay; - } + - //... +}
1 < android:name= ". Service. Backgroundservice "/>
3. Bind service in activity or fragment
1 Private voidInitaidl () {2 //Initialize the serviceconnection and implement the callback method3serviceconnection=Newserviceconnection () {4 5 @Override6 Public voidonservicedisconnected (componentname name) {7Say =NULL;8 }9 Ten @Override One Public voidonserviceconnected (componentname name, IBinder service) { A //This method is executed with a callback when the service binding succeeds -Say =ISay.Stub.asInterface (service); - } the }; -}
Private void StartService () { new Intent (mainactivity. this, Backgroundservice. class ); This . Bindservice (Intent, serviceconnection, context.bind_auto_create); This . StartService (intent); }
4. Methods for using interface definitions
Try { say. Saystring ("Hello"catch (Exception e) { e.printstacktrace ();}
5. For advanced use please refer to this blog @Copyright liuhe688
I introduced this basic use example of the code and so I know how to upload the attachment when you upload it again
-------------------------------------------------------------------------
Change 2014-5-5 15:55:34 upload attachment click to download