"Turn" Pro Android Learning note (seven or eight): Service (3): Remote service: Aidl file

Source: Internet
Author: User

Directory (?) [-]

    1. Defining the service interface in Aidl
    2. Automatically generate interface code based on the Aidl file

The articles reproduced can only be used for non-commercial nature, and can not be accompanied by virtual currency, points, registration and other additional conditions. Reprint must indicate the source: http://blog.csdn.net/flowingflying/

Remote service in previous learning notes Android learning Note (53): Service Services (next)-Remote service introduced. The remote service allows other application calls to be done, and allows RPC (remote procedure Call). Remote in Android needs to define an interface open to the client via Adil (Android Interface definition Language). Setting up a remote service requires a step:

1, write the Aidl file to define the interface open to the client, and add the Aidl file to the project src/directory.

2. Implement service, return interface in Onbind ()

3. Configure the service in Androidmanifest.xml.

Defining the service interface in Aidl

We will also learn through a simple small example. The example is simple, the remote service simulates the stock index quote, gives the request, and responds to the stock price. We will be in the src/directory, where we add Java code, add a istockquoteserice.aidl file, aidl file with Java syntax, the package name in the file is the package name of the project.

Package Cn.wei.flowingflying.proandroidservice;

Interface Istockquoteservice {
Double GetQuote (String ticker);
}

Automatically generate interface code based on the Aidl file

After compiling, the system generates the corresponding Istockquoteservice.java file according to the Aidl file, and the interface is expressed in Java. For some complex interfaces, Android automatically generates the appropriate Java files for easy use by developers.

Let's take a look at the automatically generated interface file Istockquoteservice.java. Auto-generated code is not so good-looking, with Eclipse's aggregation feature, there are two parts in Istockquoteservice, one is class stub and the other is the interface function GetQuote () we define in Aidl.

/*The interface code is generated automatically based on our definition of interface interface istockquoteservice{} in the Aidl file Istockquoteservice
* This file is auto-generated. Do not MODIFY.
* Original file: ... \\src\\cn\\wei\\flowingflying\\proandroidservice\\IStockQuoteService.aidl
*/
Package cn.wei.flowingflying.proandroidservice;/* This is why the package name in Aidl is consistent with Project */
Public interface Istockquoteservice extends Android.os.IInterface

{
/**there is an abstract class stub that integrates binder while implementing istockquoteservice. Stub is critical in remote services, when client connects to server, that is, Bindservice (), the server returns a stub object that the client can use to access the interface because of the stub implementation interface. Local-side IPC implementation stub class. */
Public static abstract class Stub extends Android.os.Binder implements Cn.wei.flowingflying.proandroidservice.IStockQuoteService
{
Private static final Java.lang.String descriptor = "Cn.wei.flowingflying.proandroidservice.IStockQuoteService";
/** Construct The stub at attach it to the interface. */
Public Stub () {...}
/** Cast an IBinder object to an Cn.wei.flowingflying.proandroidservice.IStockQuoteService interface, generating a ProX Y if needed. */
public static Cn.wei.flowingflying.proandroidservice.IStockQuoteService asinterface (Android.os.IBinder obj) {...}
@Override public Android.os.IBinder Asbinder () {...}
@Override public boolean ontransact (int code, ANDROID.OS.PARCEL data, android.os.Parcel reply, int flags) throws ANDROID.O s.remoteexception {...}
/* Proxy is the internal class of the stub, implements the Istockquoteservice interface, and makes a concrete implementation of the GetQuote () in the interface.
private static class Proxy implements Cn.wei.flowingflying.proandroidservice.IStockQuoteService

{
... ...
/* This is the implementation of the GetQuote () in the class stub, the specific code does not understand, can be understood as the service and stub between the GetQuote () of the results of the delivery, you can get the results of the service getquote () * *
@Override public double GetQuote (java.lang.String ticker) throws Android.os.RemoteException
{

Android.os.Parcel _data = Android.os.Parcel.obtain ();
Android.os.Parcel _reply = Android.os.Parcel.obtain ();
Double _result;
try {
_data.writeinterfacetoken (descriptor);
_data.writestring (ticker);
Mremote.Transact(Stub.transaction_getquote, _data, _reply, 0);
_reply.readexception ();
_result = _reply.readdouble ();
}
finally {
_reply.recycle ();
_data.recycle ();
}
return _result;
}
}
static final int transaction_getquote = (Android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
}
Public Double GetQuote (java.lang.String ticker) throws android.os.RemoteException;
}

This note covers example code that can be downloaded in the Pro Android Learning: Android Service Small example.

RELATED Links: My Android development related articles

"Turn" Pro Android Learning note (seven or eight): Service (3): Remote service: Aidl file

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.