This article is mainly about how to use the IPC in the as environment, rather than the main aidl and IPC, this part of the content can refer to other articles, or official document http://developer.android.com/guide/components/aidl.html. Aidl. Because the practice that I see on the internet is in the eclipse environment, I try to share it with you.
What is Aidl
Aidl allows you to define the programming interface this both the client and service agree upon in order to communicate WI Th each of the other using interprocess communication (IPC).
Simply put, Aidl is a way for Android to implement IPC.
Using Aidl is necessary only if you allow clients from different applications to access your service for IPC and want to H Andle Multithreading in your service. If you don't need to perform concurrent IPC across different applications, you should create your interface by Implementi ng a Binder or, if you want to perform IPC, but does not need to handle multithreading, implement your interface using a Mes Senger. Regardless, be sure this understand Bound Services before implementing an aidl.
Use Aidl only if you need different apps to access your services and need to deal with multithreading issues, otherwise you should either inherit binder directly or use Messenger.
Use of aidl as an environment
As version 1.2.2
STEP1: Create a new Aidl file on the server side
The as automatically generates a package and a aidl file
The interface that declares this aidl file
Step2:make Project
STEP3: Write a service class
PackageCom.example.kenjxli.aidlserver;ImportAndroid.app.Service;ImportAndroid.content.Intent;ImportAndroid.os.IBinder;ImportAndroid.os.RemoteException;ImportAndroid.util.Log; Public class myservice extends Service { PrivateImyaidlinterface.stub Mbinder =NewImyaidlinterface.stub () {@Override Public void Basictypes(intAnint,LongAlong,BooleanAboolean,floatAfloat,DoubleAdouble, String astring)throwsRemoteException {}@Override Public int Add(intAintbthrowsremoteexception {returnA + b; } }; Public MyService() { }@Override PublicIBinderOnbind(Intent Intent) {//Todo:return The communication channel to the service.LOG.E ("TAG","Onbind");returnMbinder; }@Override Public void OnDestroy() {Super. OnDestroy (); LOG.E ("TAG","Ondestory"); }}
STEP4: Add an Action node under the service node
<service android:name=".MyService"> <intent-filter> <action android:name="com.example.kenjxli.aidlserver"></action> </intent-filter> </service>
STEP5: Copy this directory to client Module/src/main
这是关键一步, the Aidl package that the server Module generates in STEP2 is copied directly to the Client Module/src/main.
Step6:client Module for Make Project
Step7:bindservice for IPC
Public class mainactivity extends actionbaractivity { PrivateServiceconnection connection =NewServiceconnection () {@Override Public void onserviceconnected(componentname name, IBinder service) {Imyaidlinterface aidl = IMyAidlInterface.Stub.asInterface (service);Try{intresult = Aidl.add (1,2); LOG.E ("TAG", result +""); }Catch(RemoteException e) {E.printstacktrace (); } }@Override Public void onservicedisconnected(componentname name) { } };@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Intent Intent =NewIntent ("Com.example.kenjxli.aidlserver"); Bindservice (Intent, connection, bind_auto_create); }}
End
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IPC with Aidl under Android:as environment