Service side: Only Service, no interface
1. Write the interface file, copy it to the. aidl file, and remove the modifiers such as public in it. The system will automatically generate the corresponding Java file in the Gen directory (corresponding to the interface file in the local call)
2. Write the service, where the custom bind of the inner class only needs to inherit the stub. (Local calls need to inherit bind and implement the interface interface)
1 Public classPayserviceextendsService {2 3 @Override4 Publicibinder onbind (Intent Intent) {5 return NewPay ();6 }7 Public classPayextends Stub {8 @Override9 Public voidPay (string userName, string password, string sendtoaccount,floatMoneythrowsRemoteException {Ten topay (userName, password, sendtoaccount, money); One } A } - Public voidTopay (string userName, string password, string sendtoaccount,floatMoney ) { -System.out.println (UserName + "@" + password + "to" + Sendtoaccount + "$" +Money ); the } - -}
3. Register in the manifest file and add the Intent-filter entry to facilitate the call
<service android:name= "Cn.pay.service.PayService" >
<intent-filter>
<action android:name= "Cn.pay.service.PayService"/>
</intent-filter>
</service>
======================================================== Divider Line ==================================================== ====
Client:
Customize the inner class of the serviceconnection in activity. The interface class name is used for the method of the conversion interface instance. Stub.asinterface (Service) (local call is to direct service strong to interface instance)
1 Packagecom.example.aidlpayclient;2 3 ImportCn.pay.aidl.IPayService;4 ImportAndroid.os.Bundle;5 ImportAndroid.os.IBinder;6 Importandroid.os.RemoteException;7 Importandroid.app.Activity;8 ImportAndroid.content.ComponentName;9 ImportAndroid.content.Context;Ten Importandroid.content.Intent; One Importandroid.content.ServiceConnection; A ImportAndroid.view.Menu; - ImportAndroid.view.View; - the Public classMainactivityextendsActivity { - - @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); + A //Binding Service atconn=Newmyserviceconnection (); -Bindservice (NewIntent ("Cn.pay.service.PayService"), Conn, context.bind_auto_create); - } - - PrivateMyserviceconnection conn =NULL; - PrivateIpayservice Ipayservice; in - Private classMyserviceconnectionImplementsserviceconnection { to + @Override - Public voidonserviceconnected (componentname name, IBinder service) { theIpayservice =IPayService.Stub.asInterface (service); *System.out.println ("Bind succeeded"); $ }Panax Notoginseng - @Override the Public voidonservicedisconnected (componentname name) { +Ipayservice =NULL; A } the + } - $ Public voidPay (View v) { $SYSTEM.OUT.PRINTLN ("Begin Pay"); - Try { - //invokes a method. theIpayservice.pay ("A", "B", "C", 100); -}Catch(RemoteException e) {Wuyi e.printstacktrace (); the } -System.out.println ("End Pay"); Wu } - About}
Aidl use of methods in the remote service called by activity in Android