In the previous article Android IPC mechanism (ii) using Messenger for interprocess communication we described the use of Messenger for interprocess communication, but we can find that Messenger is a serial way to handle the information sent by the client. If a large number of messages are sent to the server, it is obviously inappropriate for the service side to still handle the client one at a later response. In addition, Messenger is used for data transfer between processes but does not satisfy cross-process method calls, and then we use AIDL to implement cross-process method calls, which we have implemented with eclipse, this time we look at Android What is the difference between using Aidl in studio.
1. Create a Aidl file
We set the directory structure of the project to Android mode, create the Aidl folder in the Java sibling directory, create a package name in the folder and apply package name consistent package
We first create a igamemanager.aidl file, which has two methods, namely Addgame and Getgamelist. (IGAMEMANAGER.AIDL)
com.example.liuwangshu.moonaidl;com.example.liuwangshu.moonaidl.Game;interface IGameManager { List<Game>getGameList(); void addGame(in Game game);}
The types of data supported in the Aidl file include:
- Basic data types
- String and Charsequence
- List: Only support ArrayList, the elements inside must be aidl supported
- MAP: Only support HashMap, the elements inside must be aidl supported
- Objects that implement the Parcelable interface
- All AIDL Interfaces
In the igamemanager.aidl we used the game this class, this class implements the Parcelable, in the Aidl file we want to import in, to see the game class. (Game.java)
PackageCom.example.liuwangshu.moonaidl;ImportAndroid.os.Parcel;Importandroid.os.Parcelable; Public class Game implements parcelable { PublicString Gamename; PublicString Gamedescribe; Public Game(String gamename,string Gamedescribe) { This. gamename=gamename; This. Gamedescribe=gamedescribe; }protected Game(Parcel in) {gamename=in.readstring (); Gamedescribe=in.readstring (); } Public Static FinalCreator<game> Creator =NewCreator<game> () {@Override PublicGameCreatefromparcel(Parcel in) {return NewGame (in); }@Override PublicGame[]NewArray(intSize) {return NewGame[size]; } };@Override Public int describecontents() {return 0; }@Override Public void Writetoparcel(Parcel dest,intFlags) {dest.writestring (gamename); Dest.writestring (Gamedescribe); }}
Not to say here how to implement the Parcelable interface, in the above Igamemanager.aidl file we used the game this class, so we also want to create game.aidl, to affirm that the game implements the Parcelable interface. (GAME.AIDL)
com.example.liuwangshu.moonaidl;parcelable Game;
This time we recompile the program, the project will automatically generate igamemanager.aidl corresponding interface file, this file generated location and Eclipse location, we adjust the project directory structure to project mode, in app–>build– >generated–>soure–>aidl–>debug Directory We found our own package name file, there is an interface file in the file Igamemanager.
Igamemanager interface file code here will not say, interested can download the source of the project to understand the next.
2. Create a service-side
Server we created two game information in the OnCreate method and created binder objects to implement the methods in the Aidl interface file and return the Binder object in the Onbind method. (Aidlservice.java)
PackageCom.example.liuwangshu.moonaidl;ImportAndroid.app.Service;ImportAndroid.content.Intent;ImportAndroid.os.Binder;ImportAndroid.os.IBinder;ImportAndroid.os.RemoteException;ImportJava.util.List;ImportJava.util.concurrent.CopyOnWriteArrayList; Public class aidlservice extends Service{ PrivateCopyonwritearraylist<game> mgamelist=NewCopyonwritearraylist<game> ();PrivateBinder mbinder=NewIgamemanager.stub () {@Override PublicList<game>getgamelist()throwsremoteexception {returnMgamelist; }@Override Public void Addgame(Game game)throwsRemoteException {mgamelist.add (game); } };@Override Public void onCreate() {Super. OnCreate (); Mgamelist.add (NewGame ("Nine Yin Canon ol","Best to play Martial arts online")); Mgamelist.add (NewGame ("Great sailing age ol","The best sailing game in the game")); }@Override PublicIBinderOnbind(Intent Intent) {returnMbinder; }}
Of course, let's not forget that this server should run in another process, configuring the service in the Androidmanifest.xml file:
<service android:name=".AIDLService" android:process=":remote"></service>
3. Client calls
Finally we call the Bindservice method in the client OnCreate method to bind the remote server, and after the binding succeeds the returned Binder object is converted to the Aidl interface, so that we can invoke the remote server method through this interface. (Aidlactivity.java)
PackageCom.example.liuwangshu.moonaidl;ImportAndroid.content.ComponentName;ImportAndroid.content.Context;ImportAndroid.content.Intent;ImportAndroid.content.ServiceConnection;ImportAndroid.os.IBinder;ImportAndroid.os.RemoteException;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportJava.util.List; Public class aidlactivity extends appcompatactivity { Private Final StaticString tag="Aidlactivity";@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITY_AIDL); Intent mintent=NewIntent (aidlactivity. This, Aidlservice.class); Bindservice (Mintent,mserviceconnection, context.bind_auto_create); }PrivateServiceconnection mserviceconnection=NewServiceconnection () {@Override Public void onserviceconnected(componentname name, IBinder service) {Igamemanager igamemanager=igamemanager.stub.asinterface (service); Game game=NewGame ("Moon Shadow Legend","Best Martial Arts stand-alone game");Try{igamemanager.addgame (game); List<game> mlist=igamemanager.getgamelist (); for(intI=0; I<mlist.size (); i++) {Game mgame=mlist.get (i); LOG.I (tag,mgame.gamename+"---"+mgame.gamedescribe); } }Catch(RemoteException e) {E.printstacktrace (); } }@Override Public void onservicedisconnected(componentname name) { } };@Override protected void OnDestroy() {Super. OnDestroy (); Unbindservice (mserviceconnection); }}
After the binding succeeds we create a new game and then call the remote server's Addgame method to add the new games, and then call the loop to print out all the games in the remote service, we run the program
Print out all the games on the remote server, so that we can successfully invoke the remote server's method on the client through Aidl.
GitHub Source Download
Android IPC Mechanism (iii) implementing cross-process method calls in Android studio using Aidl