Understanding the inter-process communication principle of Android (II)----RPC mechanism

Source: Internet
Author: User
<span id="Label3"></p>Understanding the principle of the lightweight solution RPC in the Android system, you need to review the RMI (Remote Method Invocation) in java, an easy-to-use, pure Java scenario (for distributed applications). Related to RMI knowledge, can be summed up: the RPC in Android also refer to the RMI scheme in java, here we have a detailed understanding of the RPC implementation PROCESS. The RPC mechanism in Android is to implement a process using a remote object in another process, which uses android's own aidl (interface definition Language) to make it easy for the user to define an interface as a specification, through a remote service as a proxy, The client obtains the remote object during the binding of the remote service and uses the Object. See the Following: supplement: Rpc's Other purpose is to only declare the interface and methods to the client, hide the specific implementation class, for the client to get the interface instance Directly. Instance Code: instance one: a function method that invokes a socket class remotely via a service description: in mainactivity, the method process of Myplayer (implementing the iplayer Interface) is remotely invoked by binding the MyService service class. To define a iplayer.aidl file, the ADT tool automatically generates an iplayer interface class, which is then inherited by Myplayer to the static internal abstract class in the iplayer interface class, implementing an interface method that can be called remotely by other Applications. (in this case, in order to facilitate, Mainactivity and MyService in a similar application, when implemented, can not be in the same application, as long as there is access to the MyService service, you can get iplayer interface, Then execute the interface instance METHOD) list: iplayer.aidl<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>Package com.magc.rpc;<br><br>Interface IPlayer<br>{<br><br> void SetName (String name);<br> void AddFile (String f_name);<br> String ToString ();<br>}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Program Manifest: Iplayer.java (adt automatically generated from the above Iplayer.aidl file, cannot edit the File)<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>/*<br>* This file is Auto-generated. Do not MODIFY.<br>* Original File:f:\\work\\android_app\\myrpcservice\\src\\com\\magc\\rpc\\iplayer.aidl<br>*/<br>Package com.magc.rpc;<br>Public interface IPlayer extends Android.os.IInterface<br>{<br>/** local-side IPC Implementation stub class. */<br>public static abstract class Stub extends Android.os.Binder implements Com.magc.rpc.IPlayer<br>{<br>Private static final java.lang.String descriptor = "com.magc.rpc.IPlayer";<br>/** Construct The stub at attach it to the Interface. */<br>Public Stub ()<br>{<br>This.attachinterface (this, descriptor);<br>}<br>/**<br>* Cast an IBinder object to an Com.magc.rpc.IPlayer interface,<br>* Generating a proxy if needed.<br>*/<br>public static Com.magc.rpc.IPlayer Asinterface (android.os.IBinder Obj)<br>{<br>If ((obj==null)) {<br>Return null;<br>}<br>Android.os.IInterface iin = (android.os.IInterface) obj.querylocalinterface (descriptor);<br>If (((iin!=null) && (iin instanceof Com.magc.rpc.IPlayer)) {<br>return ((com.magc.rpc.IPlayer) iin);<br>}<br>return new Com.magc.rpc.IPlayer.Stub.Proxy (obj);<br>}<br>Public Android.os.IBinder Asbinder ()<br>{<br>Return this;<br>}<br>@Override public boolean ontransact (int code, android.os.Parcel data, android.os.Parcel reply, int Flags) throws ANDROID.O S.remoteexception<br>{<br>Switch (code)<br>{<br>Case Interface_transaction:<br>{<br>Reply.writestring (descriptor);<br>Return true;<br>}<br>Case Transaction_setname:<br>{<br>Data.enforceinterface (descriptor);<br>Java.lang.String _arg0;<br>_arg0 = data.readstring ();<br>This.setname (_arg0);<br>Reply.writenoexception ();<br>Return true;<br>}<br>Case Transaction_addfile:<br>{<br>Data.enforceinterface (descriptor);<br>Java.lang.String _arg0;<br>_arg0 = data.readstring ();<br>This.addfile (_arg0);<br>Reply.writenoexception ();<br>Return true;<br>}<br>Case Transaction_tostring:<br>{<br>Data.enforceinterface (descriptor);<br>Java.lang.String _result = This. ToString ();<br>Reply.writenoexception ();<br>Reply.writestring (_result);<br>Return true;<br>}<br>}<br>return super.ontransact (code, data, reply, flags);<br>}<br>private static class Proxy implements Com.magc.rpc.IPlayer<br>{<br>Private Android.os.IBinder mremote;<br>Proxy (android.os.IBinder Remote)<br>{<br>Mremote = remote;<br>}<br>Public Android.os.IBinder Asbinder ()<br>{<br>Return mremote;<br>}<br>Public java.lang.String Getinterfacedescriptor ()<br>{<br>Return descriptor;<br>}<br>public void SetName (java.lang.String Name) throws Android.os.RemoteException<br>{<br>Android.os.Parcel _data = Android.os.Parcel.obtain ();<br>Android.os.Parcel _reply = Android.os.Parcel.obtain ();<br>try {<br>_data.writeinterfacetoken (descriptor);<br>_data.writestring (name);<br>Mremote.transact (stub.transaction_setname, _data, _reply, 0);<br>_reply.readexception ();<br>}<br>finally {<br>_reply.recycle ();<br>_data.recycle ();<br>}<br>}<br>public void AddFile (java.lang.String F_name) throws Android.os.RemoteException<br>{<br>Android.os.Parcel _data = Android.os.Parcel.obtain ();<br>Android.os.Parcel _reply = Android.os.Parcel.obtain ();<br>try {<br>_data.writeinterfacetoken (descriptor);<br>_data.writestring (f_name);<br>Mremote.transact (stub.transaction_addfile, _data, _reply, 0);<br>_reply.readexception ();<br>}<br>finally {<br>_reply.recycle ();<br>_data.recycle ();<br>}<br>}<br>Public java.lang.String ToString () throws Android.os.RemoteException<br>{<br>Android.os.Parcel _data = Android.os.Parcel.obtain ();<br>Android.os.Parcel _reply = Android.os.Parcel.obtain ();<br>Java.lang.String _result;<br>try {<br>_data.writeinterfacetoken (descriptor);<br>Mremote.transact (stub.transaction_tostring, _data, _reply, 0);<br>_reply.readexception ();<br>_result = _reply.readstring ();<br>}<br>finally {<br>_reply.recycle ();<br>_data.recycle ();<br>}<br>Return _result;<br>}<br>}<br>static final int transaction_setname = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);<br>static final int transaction_addfile = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);<br>static final int transaction_tostring = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);<br>}<br>public void SetName (java.lang.String Name) throws android.os.RemoteException;<br>public void AddFile (java.lang.String F_name) throws android.os.RemoteException;<br>Public java.lang.String ToString () throws android.os.RemoteException;<br>}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Program Manifest: Myplayer.java (implements iplayer static internal abstract Stub)<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>Package com.magc.rpc;<br><br>Import android.os.RemoteException;<br>Import android.util.Log;<br><br>Import com.magc.rpc.IPlayer.Stub;<br>/**<br><br>* @author MAGC<br>* Implement the static internal abstract class in the iplayer interface class, that is, implement the iplayer interface method<br>* Future execution methods for other applications to be invoked remotely<br>*/<br>public class Myplayer extends Stub {<br><br> Private String name= "";<br> @Override<br> public void AddFile (String FName) throws RemoteException {<br> <br> System.out.println ("add File ...");<br><br> }<br><br> @Override<br> public void SetName (String Name) throws RemoteException {<br><br> THIS.name = name;<br> LOG.I ("magc", "setname--" +name);<br> }<br> <br> Public String ToString ()<br> {<br> String str = "myplayer--" +name;<br> LOG.I ("magc", "myplayer--" +name);<br> Return str;<br> }<br><br>}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>List of programs: Myservice.java (a service class for other programs to bind remotely, return to the iplayer Interface)<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>Package com.magc.rpc;<br><br>Import com.magc.rpc.IPlayer.Stub;<br><br>Import android.app.Service;<br>Import android.content.Intent;<br>Import android.os.IBinder;<br><br><br>/**<br><br>* @author MAGC<br>* This service class acts as a proxy role for other applications to bind and return interface instances<br><br>* Can be regarded as the application of proxy mode<br>*/<br>public class MyService extends Service {<br><br> Private Stub player = new Myplayer ();<br> @Override<br> Public IBinder Onbind (Intent arg0) {<br> Return player;<br> }<br><br> @Override<br> public void OnCreate () {<br> Super.oncreate ();<br> }<br><br>}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Program Manifest: Mainactivity.java (remote call to iplayer interface method as Client)<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre>Package com.magc.rpc;<br><br>Import android.app.Activity;<br>Import android.content.ComponentName;<br>Import android.content.Intent;<br>Import android.content.ServiceConnection;<br>Import android.os.Bundle;<br>Import android.os.IBinder;<br>Import android.os.RemoteException;<br>Import android.util.Log;<br>/**<br>*<br>* @author MAGC<br>* As a client by binding MyService service, implement Remote Call iplayer interface method<br>*<br>*/<br>public class Mainactivity extends Activity {<br>Private String action= "com.magc.rpc.action.MYSERVICE";<br>Private IPlayer player;<br>/** called when the activity is first Created. */<br>@Override<br>public void OnCreate (Bundle Savedinstancestate) {<br>Super.oncreate (savedinstancestate);<br>Setcontentview (r.layout.main);<br>Intent Intent = new Intent ();<br>Intent.setaction (ACTION);<br>Binding MyService Services<br>Bindservice (intent, conn, bind_auto_create);<br> <br> <br>}<br>Private Serviceconnection conn = new Serviceconnection () {<br> <br>@Override<br>public void onservicedisconnected (componentname Name) {<br> <br>}<br> <br>/**<br>* After binding the MyService service, return the iplayer interface and call the interface method<br>*/<br>@Override<br>public void onserviceconnected (componentname name, ibinder Service) {<br>LOG.I ("magc", "bind Service ...");<br>Player = IPlayer.Stub.asInterface (service);<br>If (player!=null)<br>{<br>try {<br>Player.setname ("magc");<br>Player. ToString ();<br>} catch (remoteexception E) {<br>TODO auto-generated Catch block<br>E.printstacktrace ();<br>}<br>}<br>}<br>};<br>}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>Program Listing: androidmanifest.xml (registering activity and Service)<span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><?xml version= "1.0" encoding= "utf-8"?><br><manifest xmlns:android= "http://schemas.android.com/apk/res/android"<br>Package= "com.magc.rpc"<br>Android:versioncode= "1"<br>Android:versionname= "1.0" ><br>&LT;USES-SDK android:minsdkversion= "9"/><br><br><application android:icon= "@drawable/icon" android:label= "@string/app_name" ><br><activity android:name= ". Mainactivity "<br>Android:label= "@string/app_name" ><br><intent-filter><br><action android:name= "android.intent.action.MAIN"/><br><category android:name= "android.intent.category.LAUNCHER"/><br></intent-filter><br></activity><br><br><service android:name= ". MyService "><br><intent-filter><br><action android:name= "com.magc.rpc.action.MYSERVICE"/><br><category android:name= "android.intent.category.DEFAULT"/><br></intent-filter><br></service><br></application><br></manifest></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span>The results of the above Android application run are as Follows: 1. Focus on understanding the definition of Aidl file in Android and understanding the interface class iplayer automatically generated by the ADT tool, especially its static internal stub and Asinterface method , 2, service as a proxy role, in other applications through the Asinterface method of the stub class can be implemented to return the interface instance when bound to a service, and then the instance is related Operations.<p><p>Understanding the inter-process communication principle of Android (II)----RPC mechanism</p></p></span>

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.