Android Apidemos Sample resolution: App->service->remote Service Binding

Source: Internet
Author: User
Tags call back thread

This example and the next example of remote Service Controller related documents are Remoteservice.java, Iremoteservice.aidl, Iremoteservicecallback.aidl and Isecondary.aidl.

The Android Interface definition Language (aidl) is similar to the IDL of some other systems that support remote method invocation RMI, which defines the use of interface conventions between service and client. Such remote calls are generally implemented through interprocess communication mechanisms (IPC). In the Android system, a process (processes) usually cannot directly access the memory space of other processes, and the Android system supports the use of Aidl to implement calls to service interfaces between different processes.

Before you design the Aidl interface, be aware that using the Aidl interface is done through direct function calls, but the thread threads that occur with this remote invocation differ depending on whether the caller and the service provider belong to the same process as the user:

If the caller and service belong to the same process (which can be called a local process), then aidl call will use the same thread as the caller to execute. So if your application uses only local process to access aidl call, then there is no need to use the Aidl interface, use binder, and see the Android Apidemo example parsing (i):app->service-> Local Service Binding.

If you use the remote process method to invoke Aidl, Android will distribute the function calls using the thread pool, which is managed by this process. So your service needs to be able to handle multiple process trigger aidl call, in other words, the implementation of the Aidl interface must be thread-safe.

Keyword OneWay can modify the behavior of a remote invocation, and when the OneWay keyword is used, it returns immediately after the remote call invocation, somewhat like an asynchronous invocation.

The steps for defining the Aidl interface are as follows:

The Aidl interface definition is defined in the. aidl file using the Java interface syntax and must then be placed in both the service and the client's SRC directory. When compiling with Eclipse, the Android SDK tool automatically generates the corresponding IBinder interface definition (defined in the Gen directory) based on the interface definitions of the. Aidl. The service must be implemented by this IBinder interface definition. The client can then access these methods by binding the service.

1. Create. Aidl file

The Aidl interface definition uses the same syntax as the Java interface, where each. aidl file can define only one calling interface and can only define an interface method and not contain a static variable definition. The aidl default supports int, long, char, Boolean, String, Charsequence, List, Map variable types, or other. Aidl defined types.

Here is the definition of iremoteservice.aidl,

Package Com.example.android.apis.app;     
Import Com.example.android.apis.app.IRemoteServiceCallback;     
/**    
 * Example of defining a interface for calling on to a remote service    
 * (running in another process). 
* * Interface Iremoteservice {     
 /** * often you want to    
 allow a service    
 to the call back to its clients. * This shows the registering a callback interface with    
 * the service.    
 * * 
 void RegisterCallback (Iremoteservicecallback cb);     
 /**    
 * Remove a previously registered callback interface.    
 * * 
 void Unregistercallback (Iremoteservicecallback cb); <br/>     
}

At compile time, the Android SDK tool automatically generates the corresponding Iremoteservice.java in the Gen directory.

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.