Android Learning Note service with remote communication ... (aidl)

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>PS: This chapter looks at me a bit confused, not very easy to understand ... But fortunately, I finally understand the more than half ... The basic puzzle is solved ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Learning Content:</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>1. Cross app Launch service ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>2. Cross App Binding service ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>3. Cross-application Implementation Communication ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Since the 5.0 version before and 5.0 is very different, so I will be here to introduce the specific ...</strong></span></p></p><p><p></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>1. Cross app Launch service ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Starting a service across apps is actually a kind of communication between multiple applications, where the application can communicate or do some other interaction, and in a nutshell, start a service from another application in my own application, which completes the Cross-app launch service ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>2. Cross App Binding service ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>The basic similarity between Application-bound services and Cross-app launch services is that they implement bindings across applications, calling functions that are different ... I do not paste the code here, because there is no need, just cross-application launch and binding service is meaningless, because the application only to achieve communication, then the cross-application is necessary, otherwise I light binding or just start the service, and can not complete the communication, it is meaningless ... So the focus is still the following point ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>3. Cross-application Implementation Communication ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Communication is implemented across applications, this thing is very important, and between applications, it is inevitable that communication is needed ... The completion of communication here is generally done using the binding service, that is, using Bindservice () to Complete. It's Here. The android mechanism provides us with a aidl service to facilitate cross-process communication, so what is aidl, the Android interface Language. It is used to expose the Service's interface, and then other applications can also access the service within the Application: is a very fierce mechanism ... So how to use it ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Here I build two applications, one is server, one is client ... The server is used to provide data that the client uses to obtain data ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>I. First we set up a service-side ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Create a new project ... exam_cilent, the package name is Org.hnist.demo. Then click on the package file, new->file, then pop up the window and enter iperson.aidl at the BOTTOM. Direct finish on the line ... Then edit our Aidl file.</strong></span></p></p><pre><pre><span style="color: #0000ff;"></span> package <span style="color: #000000;">org.hinst.demo;</span> <span style="color: #0000ff;">Interface</span> <span style="color: #000000;">iperson{ </span><span style="color: #0000ff;">void</span> <span style="color: #000000;"> setName (String name); </span> <span style="color: #0000ff;">void</span> setage (<span style="color: #0000ff;">int</span> age<span style="color: #000000;"> ); </span> <span style="color: #0000ff;">void</span> <span style="color: #000000;">setemail (String email); </span></pre></pre><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Under normal circumstances, a Iperson.java file will be generated automatically under the R.java file ... This takes a little bit of time ... If there is no build, then rebuild project ... And then it's done ...<br></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Then build a Ipersonimp.java file, This file needs to inherit the Iperson.stub class, here is the core part ... It must be understood that we have defined an interface that must have a class to implement this Interface. This class is something that can really be called by the Client.</strong></span></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">org.hinst.demo;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.RemoteException;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Ipersonimpl<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">iperson.stub{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>String name= "clearlove"<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Age=20<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>String email= "[email protected]"<span style="color: #000000;"><span style="color: #000000;">; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>SetName (String Name)<span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">RemoteException {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. name=<span style="color: #000000;"><span style="color: #000000;">name; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Setage (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Age<span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">RemoteException {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. age=<span style="color: #000000;">age <span style="color: #000000;">; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Setemail (String Email)<span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">RemoteException {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. email=<span style="color: #000000;"><span style="color: #000000;">email; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>String Display ()<span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">RemoteException {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>"name:" +name+ "age" +age+ "mailbox" +<span style="color: #000000;"><span style="color: #000000;">email; }}</span></span></pre><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>The Mainactivity.java file on the server is kept intact ... Anything is not added or can ... You can also define some code to invoke the service Yourself:<br></strong></span></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>Finally we need to do some corresponding configuration in the Androidmanifest.xml file ... This file is required to be configured, otherwise there will be a problem ... Just add the following code between </activity> and </application> to complete ... This is allowed before the 5.0 release, but it is not allowed to do this again after the 5.0 version ...</strong></span></strong></span></p></p><pre><pre> <span style="color: #0000ff;">< </span> <span style="color: #800000;">service </span> <span style="color: #ff0000;">android:name </span> <span style="color: #0000ff;">= "myremoteservice" </span> <span style="color: #0000ff;">> </span> <span style="color: #0000ff;">< </span> <span style="color: #800000;">intent-filter </span> <span style="color: #0000ff;">> </span> <span style="color: #0000ff;">< </span> <span style="color: #800000;">action </span> <span style="color: #ff0000;">android:name </span> <span style="color: #0000ff;">= "org.hinst.demo.MyRemoteService" </span> <span style="color: #0000ff;">/> </span> <span style="color: #0000ff;"></</span> <span style="color: #800000;">intent-filter </span> <span style="color: #0000ff;">> </span> <br> <span style="color: #0000ff;"></</span> <span style="color: #800000;">service </span> <span style="color: #0000ff;">> </span> </pre></pre><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>Ii. so we're done with the Server. Next is the client ... The client is relatively simple .... Just pay attention to a little detail ... We set up another project ... Package name Org.exam ... Then create a new Package. This package is used to store ... Server-side aidl File ... The package name is the same as the package name of the Adil file saved by the client, which is org.hnist.demo ... Then create a new Aidl file again, Enter the same content as the server ... When finished, the R.java file on the client will also generate a file ... So the Client's Aidl is configured ...<br></strong></span></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>Then the client makes the call ... Make a call in mainactivity ...</strong></span></strong></span></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">org.hnist.exam;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">org.hinst.demo.IPerson;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">Com.example.exam6_7client. R;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.Bundle;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.IBinder;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.os.RemoteException;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.Activity;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.app.Service;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.content.ComponentName;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.content.Intent;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.content.ServiceConnection;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.Menu;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.view.View.OnClickListener;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">android.widget.TextView;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Mainactivity<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Activity<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">View.onclicklistener {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">IPerson iperson; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">TextView TextView; Intent Intent</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Intent ("org.hinst.demo.MyRemoteService"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Serviceconnection con=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">serviceconnection () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onservicedisconnected (componentname Name) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>iperson=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">synchronized</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onserviceconnected (componentname name, ibinder service) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>iperson=<span style="color: #000000;"><span style="color: #000000;">IPerson.Stub.asInterface (service); } }; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Findviewbyid (r.id.start). Setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Bindservice (intent, con, service.bind_auto_create); TextView</span></span>=<span style="color: #000000;"><span style="color: #000000;">(TextView) Findviewbyid (r.id.show); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span><span style="color: #000000;"><span style="color: #000000;">Oncreateoptionsmenu (menu Menu) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Inflate the menu; this adds items to the action bar if it is Present.</span></span><span style="color: #000000;"><span style="color: #000000;">getmenuinflater (). Inflate (r.menu.main, menu); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(v.getid ()) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">r.id.start:</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{iperson.setname (</span></span>"clearlove"<span style="color: #000000;"><span style="color: #000000;">); String msg</span></span>=<span style="color: #000000;"><span style="color: #000000;">Iperson.display (); Mainactivity.</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">. Textview.settext (msg); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(remoteexception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); } } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnDestroy () {unbindservice (con); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnDestroy (); }}</span></span></pre><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>Layout file I will not be pasted .... We first start the server to complete the corresponding configuration of the Aidl file, and then call the client, so that when the trigger button, the client will output the service side of the information ... This completes the Client-to-server communication ...<br></strong></span></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>After the 5.0 release, we cannot use the configuration Androidmanifest.xml file, The implicit intent has been unable to complete the communication between the multiple applications ... After version 5.0: Aidl file configuration is the same, there is no change, the only change is the need to use explicit intent, to pass ... I really don't know the reason for this ...</strong></span></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>How to pass it? That's it...</strong></span></strong></span></p></p><pre><pre>Serviceintent =<span style="color: #0000ff;">new</span> <span style="color: #000000;"> Intent (); serviceintent.setcomponent (</span><span style="color: #0000ff;">new</span> componentname (" Com.example.myserviceapp "," Com.example.myserviceapp.AppMyService "));</pre></pre><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>As long as the intent created by the object set a primary key value, the primary key value has two parameters, one is the service we want to call the package name, one is the service name .... Then this completes the assignment of the primary key value .... Then use the Bindservice (serviceintent,this,server.bind_auto_create) method again to pass ... Is finished ... Finally put a complete code, convenient for everyone to understand ... A packaged mechanism is used here: You don't have to understand. The key is to look at the server and the implementation of the client ... In fact, my above code is also possible to run ... The above is more simple ...<br></strong></span></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>This cross-process communication is not very good understanding, in fact, it is not difficult, as long as the understanding will find, in fact, there is no difficult thing ... The key is to see whether you study hard ...</strong></span></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong><span style="font-family: 楷体; font-size: 16px;"><strong>This is the Service-side code ...</strong></span></strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Http://files.cnblogs.com/files/RGogoing/AIDLService.rar</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>This is the client ...</strong></span></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong>Http://files.cnblogs.com/files/RGogoing/AIDLClient.rar</strong></span></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-family: 楷体; font-size: 16px;"><strong></strong></span></p></p><p><p></p></p><p><p> Android Learning Note service services with remote communication ... (aidl) </p></p></span>
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.