Analysis of remote service in Android

Source: Internet
Author: User

The previous article simply wrote about the two ways to start a service in Android, but both are local services, today simply write down the use of remote service in Android, learn two concepts before learning, Aidl (Android Interface definition Language) literally means an excuse to define a language, and a professional understanding is an excuse for communicating between Android processes. IPC (inter-process conmmunication) communication between internal processes, on the same phone, if your app needs to access a service that calls another app, the way it communicates is IPC.

service calls in the same app

Unlike the previous article, this time create your own aidl file named Bookaidlservice.aidl:

Package Com.remote.service;interface bookaidlservice {    int  sum (int a,int b);}

Spit Groove, a lot of online is so written, their new time did not find how to create a new Aidl file, you first need to build a Bookaidlservice.java file, and then modify the suffix named Aidl, this time to see the effect as follows:

After saving, it will automatically generate a Bookaidlservice.java file in the Gen directory, or, as in the beginning, look at the application page:

The local event is for the third button, first to rewrite the following bookservice:

public class Bookservice extends Service {private String tag = "Bookservice"; Bookaidlservice.stub bookaidlbinderstub=new Stub () {@Overridepublic int sum (int a, int b) throws RemoteException {//TODO Auto-generated method Stubreturn a+b;}}; @Overridepublic void OnCreate () {//TODO auto-generated method Stubsuper.oncreate (); LOG.I (Tag, "Start OnCreate started"); LOG.I ("Bookservice", "Bookservice ID:" +process.mypid ()),//try {//thread.sleep (40000);/} catch ( Interruptedexception e) {////TODO auto-generated catch Block//e.printstacktrace ();/}} @Overridepublic int Onstartcommand (Intent Intent, int flags, int startid) {//TODO auto-generated method STUBLOG.I (tag, "Start execution Onstartcommand start "); Toast.maketext (This, "Bookservice begins", Toast.length_short). Show (); return Super.onstartcommand (Intent, flags, Startid);} @Overridepublic void OnDestroy () {//TODO auto-generated method STUBLOG.I (tag, "destroy OnDestroy initiated"); Super.ondestroy ();} @Overridepublic ibinder onbind (Intent Intent) {//TODO auto-generated method StuBLOG.I (Tag, "Bind Onbind started"); return bookaidlbinderstub;} @Overridepublic boolean onunbind (Intent Intent) {//TODO auto-generated method STUBLOG.I (Tag, "Unbind Onunbind started"); return Super.onunbind (intent);} Class Bookbinder extends Binder {public Bookservice getcurrentservice () {return bookservice.this;}}}

The biggest difference from the previous one is to return a bookaidlbinderstub in the Onbind method, while the last written bookconnection should also flush and write:

    Class Bookserviceconnection implements Serviceconnection{private Bookaidlservice Bookaidlservice;public Bookserviceconnection () {super ();//TODO auto-generated constructor stub} @Overridepublic void Onserviceconnected ( ComponentName name, IBinder service) {//TODO auto-generated method stub//Get instance//bookservice  bookservice= (( Bookservice.bookbinder) service). Getcurrentservice ();//just  do  wo want to do  Bookaidlservice  bookaidlservice=bookaidlservice.stub.asinterface (service);  try {int result=bookaidlservice.sum (10, 100); LOG.I ("Bookservice", "Bookaidlservice call Result:" +result);} catch (RemoteException e) {//TODO auto-generated catch Blocke.printstacktrace ();}  } @Overridepublic void onservicedisconnected (componentname name) {//TODO auto-generated Method stub}}}        

The foreground call is:

Intent binderstartintent=new Intent ("Com.example.googleservice.BookService.AIDL");  Connection=new bookserviceconnection (); Bindservice (Binderstartintent, connection,context.bind_auto_create);

Here intent is implicitly called, if not very familiar can refer to my previous article, androidmanifest.xml file need to re-modify:

       <service android:name= "Com.example.googleservice.BookService"            android:process= ": remote" >            < intent-filter>               <action android:name= "Com.example.googleservice.BookService.AIDL"/>            </ Intent-filter>        </service>

 The result of the call is as follows:

calls between different apps

The call between the different, due to each other to communicate with each other, the same need to define the server side of the aidl name of the same aidl, create a new Android project, and then the structure is as follows:

The client page does not have to write, just a call button, the client to the server bookconnection copy come over:

   Class Bookserviceconnection implements Serviceconnection{private Bookaidlservice Bookaidlservice;public Bookserviceconnection () {super ();//TODO auto-generated constructor stub} @Overridepublic void Onserviceconnected ( ComponentName name, IBinder service) {//TODO auto-generated method stub//Get instance//bookservice  bookservice= (( Bookservice.bookbinder) service). Getcurrentservice ();//just  do  wo want to do  Bookaidlservice  bookaidlservice=bookaidlservice.stub.asinterface (service);  try {int result=bookaidlservice.sum (10, 100); LOG.I ("Bookservice", "Client Bookaidlservice call Result:" +result);} catch (RemoteException e) {//TODO auto-generated catch Blocke.printstacktrace ();}  } @Overridepublic void onservicedisconnected (componentname name) {//TODO auto-generated method stub}        

Client calls:

Intent binderstartintent=new Intent ("Com.example.googleservice.BookService.AIDL");  Connection=new bookserviceconnection (); Bindservice (Binderstartintent, connection,context.bind_auto_create);

The result of the call is as follows:

Well, to this point briefly on the Android remote service invocation, a lot of concepts do not speak, do not drop the book bag, there is an interest in their own private understanding under the different processes to pass data, Android to this kind of data format support is very limited, basically can only pass the basic Java data type, String, list, or map, if you want to pass a custom class, you have to let the class implement the Parcelable interface, and you want to define a aidl file with the same name for the class. Similar, you can study on their own, accidentally and Friday, hey ~

Analysis of remote service in Android

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.