Use instances of interprocess communication---aidl

Source: Internet
Author: User
Tags stub

Aidl (Android Interface Definition Language) is an IDL language used to generate interprocess communication between two processes on Android devices (interprocess communication, IPC) The code. You can use Aidl to generate serializable parameters if you want to invoke the operation of another process (such as a service) object in one process (for example, activity). This article briefly introduces the use of Aidl.

1. New Iremoteservice.aidl

Package Com.tang.remoteservicedemo;interface Iremoteservice {String getInfo ();}
As you can see from the content, this thing is like an interface. Now that we've defined such a thing, we're going to implement it.

2. New IService "implement" Iremoteservice "interface"

Package Com.tang.remoteservicedemo;import Java.util.date;import Android.app.service;import android.content.Intent; Import Android.os.ibinder;import Android.os.remoteexception;public class IService extends Service{private IBinder IBinder = new Iremoteservice.stub () {@Overridepublic String getInfo () throws RemoteException {//TODO auto-generated Metho D Stubreturn New Date (System.currenttimemillis ()). toLocaleString () + "  information from remote services!" ";}}; @Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method Stubreturn IBinder;}}
Based onBinderCommunication between the different processes,client and service in different processes, in the case of the user program, when the service returned to the IBinder interface, access to the method in the service is like calling its own functionThe same.

3. Configuring IService for other process calls

        <service android:name= "Com.tang.remoteservicedemo.IService"            android:process= ": remote" >            < intent-filter>                <action android:name= "Com.tang.remoteservicedemo.IService"/>            </intent-filter >        </service>

Configure a owning process name and an action.

With the previous three steps, the service that contains the GetInfo () method can be called to someone else, which is called by the client below

4. Create a new Clientdemo project will contain iremoteservice.aidl all copies of the package to SRC, leaving only aidl files, the other all deleted.


5. Create a new mainactivity, the other is bound service operation

Package Com.tang.clientdemo;import Java.util.timer;import Java.util.timertask;import Com.tang.remoteservicedemo.iremoteservice;import Android.app.activity;import Android.content.ComponentName; Import Android.content.context;import Android.content.intent;import Android.content.serviceconnection;import Android.os.bundle;import Android.os.ibinder;import Android.os.remoteexception;import Android.util.Log;public class Mainactivity extends Activity {private Iremoteservice IService = Null;private boolean isbinded =false; Serviceconnection conn = new Serviceconnection () {@Overridepublic void onservicedisconnected (componentname name) {// TODO auto-generated Method stubisbinded = False;iservice = null;} @Overridepublic void onserviceconnected (componentname name, IBinder service) {//TODO auto-generated method Stubiservice = IRemoteService.Stub.asInterface (service); isbinded = true;}};    public void Dobind () {Intent Intent = new Intent ("Com.tang.remoteservicedemo.IService"); Bindservice (Intent, COnn, context.bind_auto_create);}      public void Dounbind () {if (isbinded) {unbindservice (conn);      IService = null;    isbinded = false; }} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); new Thread (New Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubdobind ( );}}). Start (); Timer timer = new timer (); Timer.schedule (task, 0, 2000);} TimerTask task = new TimerTask () {@Overridepublic void Run () {//TODO auto-generated method stubif (iservice!=null) {try {Lo G.I ("AAA", Iservice.getinfo ());} catch (RemoteException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} ELSE{LOG.I ("AAA", "Iservice!=null");}}; @Overrideprotected void OnDestroy () {//TODO auto-generated method Stubdounbind (); Task.cancel (); Super.ondestroy ();}}

The log after execution is as follows:

Why is there an empty log, because the binding is also time ....

The simple aidl is so much used.

SOURCE download

Use instances of interprocess communication---aidl

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.