Aidl use of methods in the remote service called by activity in Android

Source: Internet
Author: User

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

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.