Androidstudio open, bind remote service via AIDL

Source: Internet
Author: User
<span id="Label3"></p>Objective<p><p>About the way the service is started (startservice (), stopservice (), bindservice (), unbindservice ()), I don't have much to say here, so you can refer to this blog Post.</p></p>Example schematic diagram<p><p>In a simple case, this article records how to use Aidl to implement inter-process communication with the Service:<br>first, Create two projects, one project (remoteservice) as the remote service provider, and the other (remoteservicetest) As a client that invokes a remote Service. then, when the client binds the remote service, the method in the remote service can be called through the Aidl interface, and the principle process<br></p></p>Remote Service Remoteservice Project<p><p><strong>1.</strong> Create the Aidl file, Select the package name that contains the service class you want to provide, right-click the file, aidl. aidl, New--<br><br>Here the Aidl file is named remoteinterface, after creation, a aidl folder is generated under main, and a package name is the same as the package name just selected, and a remoteinterface.aidl is generated under the Package.<br></p></p><p><p><strong>2.</strong> Open the Remoteinterface.aidl file, define an interface method Remoteprintinterface (); Recompile the project, and if it compiles successfully, a remoteinterface interface file will be generated in the following directory<br><br>Note Do not modify this interface file, the interface has an important stub class, is to be used later, the stub class inherits the binder, and implements the Remoteinterface Interface.<br></p></p><p><p><strong>3.</strong> Create a service class such as Remoteservice to inherit from service, define a method Remoteprint () in the service, and define an inner class mybinder inherit from Stub, Implement the method of Remoteprintinterface () in the stub or Remoteinterface interface, let this method go to invoke the service Remoteprint () method, The code is as Follows:</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>com.yu.remoteservice;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.app.Service;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.content.Intent;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.IBinder;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.support.annotation.Nullable;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.util.Log;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.widget.Toast;<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Created by yu on 2016/4/15.</span> * *</span><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">remoteservice</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Service</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>String TAG =<span class="hljs-string"><span class="hljs-string">"remoteservice"</span></span>;<span class="hljs-annotation"><span class="hljs-annotation">@Nullable</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>IBinder<span class="hljs-title"><span class="hljs-title">Onbind</span></span>(Intent Intent) {LOG.D (TAG,<span class="hljs-string"><span class="hljs-string">"remoteservice binding------------------------------------------"</span></span>); Toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"remote Service bindings"</span></span>, toast.length_short). show ();<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Mybinder ();<span class="hljs-comment"><span class="hljs-comment">//be sure to remember to return to the Mybinder object</span></span>}<span class="hljs-comment"><span class="hljs-comment">//inner class inherits from stub</span></span>Class Mybinder extends remoteinterface.stub{<span class="hljs-comment"><span class="hljs-comment">//implement methods in Remoteinterface</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Remoteprintinterface</span></span>() {<span class="hljs-comment"><span class="hljs-comment">//method of invoking the service</span></span>Remoteprint (); } }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Remoteprint</span></span>() {LOG.D (TAG,<span class="hljs-string"><span class="hljs-string">"remoteprint method for calling Remoteservice via interface"</span></span>); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (); LOG.D (TAG,<span class="hljs-string"><span class="hljs-string">"remoteservice create------------------------------------------"</span></span>); Toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"remote Service creation"</span></span>, toast.length_short). show (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnDestroy</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. OnDestroy (); LOG.D (TAG,<span class="hljs-string"><span class="hljs-string">"remoteservice destruction------------------------------------------"</span></span>); Toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"remote Service destruction"</span></span>, toast.length_short). show (); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">Onunbind</span></span>(Intent Intent) {LOG.D (TAG,<span class="hljs-string"><span class="hljs-string">"remoteservice------------------------------------------"</span></span>); Toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"remote Service bindings"</span></span>, toast.length_short). show ();<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. Onunbind (intent); }}</code></pre><p><p><strong>4.</strong> Registering the service in the manifest file</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs xml"><span class="hljs-tag"><<span class="hljs-title">service</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">".RemoteService"</span>></span> <span class="hljs-tag"><<span class="hljs-title">intent-filter</span>></span> <span class="hljs-tag"><<span class="hljs-title">action</span> <span class="hljs-attribute">android:name</span>=<span class="hljs-value">"com.yu.remote"</span>/></span> <span class="hljs-tag"></<span class="hljs-title">intent-filter</span>></span><span class="hljs-tag"></<span class="hljs-title">service</span>></span></code></pre></pre><p><p>Through the above steps, the service side even if the code is finished, after the client binds the service, the method in the service can be invoked indirectly through the method in the Mybinder object returned by the Onbind Method.</p></p>Client Remoteservicetest Project:<p><p><strong>1.</strong> The purpose is to generate a file in main under the same remoteservice as the aidl, as Follows:<br>1) Create a Aidl folder under Main<br>2) Create a package under the Aidl folder, which is the same as the package name where the server Aidl file is located, com.yu.remoteservice<br>3) Copy the Remoteinterface.aidl file from the server to the Package.<br>4) rebuild project, After the end, should generate and serve a hair-like interface<br>Results<br></p></p><p><p><strong>2.</strong> bind the service, get the IBinder object returned by the service, strongly switch to the type of the interface, and invoke the method of the interface to call the method in the service indirectly</p></p><p><p>The client code is as Follows:</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>com.yu.remoteservicetest;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.content.ComponentName;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.content.Intent;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.content.ServiceConnection;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.IBinder;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.RemoteException;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.support.v7.app.AppCompatActivity;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.Bundle;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.util.Log;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.view.View;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.widget.Toast;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>com.yu.remoteservice.RemoteInterface;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">appcompatactivity</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>String TAG =<span class="hljs-string"><span class="hljs-string">"remoteservicetest"</span></span>;<span class="hljs-comment"><span class="hljs-comment">//the package Name of the remote service</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>String Remoteservicepackage =<span class="hljs-string"><span class="hljs-string">"com.yu.remoteservice"</span></span>;<span class="hljs-comment"><span class="hljs-comment">//connection Object when the service is bound</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Serviceconnection conn =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-comment"><span class="hljs-comment">//object of an interface class created through Aidl</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>Remoteinterface ri =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Setcontentview (r.layout.activity_main); }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Start</span></span>(view View) {Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent ();<span class="hljs-comment"><span class="hljs-comment">//set the action that the intent object will match</span></span>Intent.setaction (<span class="hljs-string"><span class="hljs-string">"com.yu.remote"</span></span>);<span class="hljs-comment"><span class="hljs-comment">After//android 5.0</span></span> <span class="hljs-comment"><span class="hljs-comment">//set Package name, tested here, must be the package name of the service</span> to</span>Intent.setpackage (remoteservicepackage); StartService (intent); }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Stop</span></span>(view View) {Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (); Intent.setaction (<span class="hljs-string"><span class="hljs-string">"com.yu.remote"</span></span>); Intent.setpackage (remoteservicepackage); StopService (intent); }<span class="hljs-comment"><span class="hljs-comment">//binding Services</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Bind</span></span>(view View) {Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (); Intent.setaction (<span class="hljs-string"><span class="hljs-string">"com.yu.remote"</span></span>); Intent.setpackage (remoteservicepackage);<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(conn = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-comment"><span class="hljs-comment">//create a Connection service object</span></span>conn =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Remoteserviceconnection (); } Bindservice (intent,conn,bind_auto_create); }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Unbind</span></span>(view View) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(conn! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {unbindservice (conn); conn =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>; } }<span class="hljs-comment"><span class="hljs-comment">//method of invoking the service</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Callremoteprint</span></span>(view View) {LOG.D (TAG,<span class="hljs-string"><span class="hljs-string">"the callremoteprint of the" ... "</span> ........"</span>);<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(ri! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{<span class="hljs-comment"><span class="hljs-comment">//by Invoking the Remoteprintinterface method of an interface object, introducing the Remoteprint method of invoking the service</span></span>Ri.remoteprintinterface (); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(remoteexception E) {e.printstacktrace (); } }<span class="hljs-keyword"><span class="hljs-keyword">Else</span></span>{toast.maketext (<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>,<span class="hljs-string"><span class="hljs-string">"remote interface is null"</span></span>, toast.length_short). show (); } }<span class="hljs-comment"><span class="hljs-comment">//implement the serviceconnection class, which is used to create the object of the connection service</span></span>Class Remoteserviceconnection implements Serviceconnection {<span class="hljs-comment"><span class="hljs-comment">//when The service is bound using the Bindservice method, and the service returns the IBinder object, it calls the</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onserviceconnected</span></span>(componentname name, IBinder Service) {LOG.D (TAG,<span class="hljs-string"><span class="hljs-string">"the onserviceconnected of the" ... "</span> ........"</span>);<span class="hljs-comment"><span class="hljs-comment">//strong to interface object</span></span>RI = RemoteInterface.Stub.asInterface (service); }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onservicedisconnected</span></span>(componentname Name) { } }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnDestroy</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. OnDestroy ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(conn! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {unbindservice (conn); conn =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>; } }}</code></pre><p><p>Client interface<br></p></p>File directory structure<p><p><strong>Client file directory structure</strong><br></p></p><p><p><strong>Remote Service file directory structure</strong><br></p></p>Problems encountered:<p><p><strong>1.</strong> After creating the Aidl file, the corresponding interface is not automatically generated, and the following error is reported after Rebuild:</p></p> <blockquote> <blockquote> <p>Error:execution failed for task ': App:compiledebugaidl '.<br>Java.lang.RuntimeException:<br>Com.android.ide.common.process.ProcessException:<br>Org.gradle.process.internal.ExecException:Process ' command<br>' F:\Study\Android\soft\adt-bundle-windows-x86_64-20140321\sdk\build-tools\24.0.0-preview\aidl.exe '<br>Finished with Non-zero exit value 1</p> </blockquote> </blockquote><p><p>search, probably said that the project compiled version and the compilation tool version inconsistent ... The egg is broken ...<br>The Final Solution Is:<br>Right-click Project Open Moudle Setting--app---modify compile SDK version and build Tools versions are the Same.</p></p><p><p><strong>2.</strong> Both StartService and Bindservice are not able to start the remote service:<br>The reason is that android5.0 after the surface is not implicitly start service, refer to this article<br>also, Intent.setpackage (remoteservicepackage) must be set to the package name where the service is located, otherwise the service cannot be turned on, and there is no error or warning given ...</p></p> <p><p>Androidstudio open, bind remote service via AIDL</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.