Android Studio Aidl Usage detailed

Source: Internet
Author: User
Tags stub

There's a lot of interprocess communication in our Android development, and Android offers a solution that's aidl, so let me explain how to implement Aidl in Android Studio.

1. Create a project in Android studio

2, create two module such as mine: Aidlserver and aidlclient so that two processes can be implemented.

3, in the aidlserver of the creation of AIDL file see the following figure













4. Declare your service method in your new aidl

5. Create service under Java/pakagename (don't forget to declare your service in mainfest)

Public classMyServiceextendsservice{@Nullable @Override PublicIBinder Onbind (Intent Intent) {return NewMybinder (); }classMybinderextendsimyaidlinterface.stub{@OverridePublic voidBasictypes (intAnint,LongAlong,BooleanAboolean,floatAfloat,DoubleAdouble, String astring)throwsremoteexception {System. out. println ("Get Data from client:"+ Anint +" " + along); } @OverridePublic voidSendobj (Student Student)throwsremoteexception {System. out. println ("Xixixixiixiixi"); }
    }
}
6, will your AIDL package, direct copy to Aidlclient (client's AIDL package must be consistent with the service end, otherwise serialization fails, cannot bindservice)
7, the implementation of client and server-side binding code

Public classMainactivityextendsappcompatactivity {PrivateImyaidlinterfaceAidlservice; @Overrideprotected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.Activity_main); } @Overrideprotected voidOnresume () {Super. Onresume (); Intent Intent =NewIntent (); Intent.setaction ("Cn.com.callServer"); Bindservice (Intent,NewMyConnection (),bind_auto_create); }Public voidCallservice (view view) {Try{Aidlservice. basictypes (15, 123,true, 887.3f, 563.5,"Hello"); Student Student =NewStudent ();
            Student.setage (12); Student.setname ("Nihao");Aidlservice. Sendobj (student); }Catch(RemoteException e)
        {E.printstacktrace (); }
    }classMyConnectionImplementsserviceconnection {@OverridePublic voidonserviceconnected (componentname name, IBinder service) {Aidlservice= IMyAidlInterface.Stub.asInterface (service); } @OverridePublic voidonservicedisconnected (componentname name) {}}}
8, you can try to run Aidlserver and aidlclient, there is no accident aidlclient can successfully invoke the method in Aidlserver.
Note: Have you noticed that there is also an interface to use the Sudent class, the following I will focus on how to use the custom object in the Aidl.
    1, first in the Aidlserver Aidl package to create the student class (why put here, so you can directly to the AIDL packet to the client to save manual copy of the Trouble)
        Student be sure to implement the Parcelable interface, the following is the student object I created
Public classStudentImplementsparcelable{PrivateStringname;PrivateStringTelnumber;private int Age; PublicStudent () {} PublicStudent (Parcel PL) {name= Pl.readstring ();Telnumber= Pl.readstring (); Age= Pl.readint (); } PublicString GetName () { returnname; }Public voidSetName (String name) { This.name= name; } PublicString Gettelnumber () { returnTelnumber; }Public voidSettelnumber (String telnumber) { This.Telnumber= Telnumber; }public intGetage () { return Age; }Public voidSetage (intAge) { This. Age= age; } @Overridepublic intDescribecontents () { return0; } @OverridePublic voidWritetoparcel (Parcel dest,intFlags) {dest.writestring (name); Dest.writestring (Telnumber); Dest.writeint ( Age); }Public static FinalCreator<student>CREATOR=NewCreator<student> () {@Override PublicStudent Createfromparcel (Parcel source) {return NewStudent (source); } @Override PublicStudent[] NewArray (intSize) {return NewStudent[size];
}

    }; }
2, under the Aidl package to create student.aidl files, the following is the specific content   
      Com.example.aidlserver;
       Student;
3, at this time you go to run your aidl will be an error, log display can not find the definition of student, right now your student under Aidl package, compile will not go under it to find classes, at this time you need to do the following work, in the Build.gradle file to add the following code. In the same vein, you should add in your aidlclient.
    
   sourcesets{
   	    main{
      	          java.srcdirs = [' Src/main/java 'src/main/aidl ']
            }  
   }
4, at this time you run should not be a problem.
Summarize:
   Above is my personal development way, if can help everybody is my honor, if everybody does not like please ignore.


Related Article

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.