How to Implement the Android Interface

Source: Internet
Author: User

In different processes, how does one implement object transfer? Obviously, cross-Process Memory sharing is not allowed in Java. Therefore, to pass objects, you can only split the objects into simple forms that the operating system can understand. The problems related to Android interfaces are described in detail below.

To achieve cross-border object access. in J2EE, RMI can be used to pass objects through serialization. in Android, AIDL is used. in theory, AIDL can pass Bundle, but it is troublesome to do it. AIDL (AndRoid Interface Description Language) is an interface description language;

The compiler can generate a piece of code through the aidl file and use a pre-defined interface to implement internal communication between two processes. if you want to access an object in another Service in one Activity, you must first convert the object to a parameter that can be recognized by AIDL (possibly multiple parameters ), then, AIDL is used to pass these parameters. At the message receiver, these parameters are used to assemble them into desired objects.

The IPC Mechanism of the AndRoid interface is similar to that of COM or CORBA and is based on interfaces, but it is lightweight. It uses the proxy class to pass values between the client and the Implementation Layer. To use AIDL, you need to do two things: 1. Introduce the relevant class of AIDL; 2. Call the class generated by aidl.

The specific implementation steps are as follows:

1. Create an AIDL file and define the interface in this file. This interface defines the methods and attributes that can be accessed by the client. For example, ITaskBinder. adil

Note: theoretically, the parameter can pass the basic data type and String, and the Bundle derived class. However, in Eclipse, the current ADT does not support Bundle as the parameter. It is said that Ant compilation is acceptable, I did not try.

2. Compile the AIDL file. If Ant is used, it may need to be manually. If Eclipse plugin is used, java files can be automatically generated and compiled based on the adil file without human intervention.

3. In the Java file, implement the interface defined in AIDL. the compiler will generate a JAVA Interface Based on the AIDL interface. This interface has an internal abstract class named Stub. It inherits several methods required to extend the interface and implement remote calls. Next, you need to implement several custom interfaces.

 
 
  1. Package com. cmcc. demo;
  2.  
  3.  
  4. Import com. cmcc. demo. ITaskCallback;
  5.  
  6.  
  7. Interface ITaskBinder {
  8.  
  9.  
  10. Boolean isTaskRunning ();
  11.  
  12.  
  13. Void stopRunningTask ();
  14.  
  15.  
  16. Void registerCallback (ITaskCallback cb );
  17.  
  18.  
  19. Void unregisterCallback (ITaskCallback cb );
  20.  
  21. }
  22.  
  23. Here, ITaskCallback is defined in the ITaskCallback. aidl file:
  24.  
  25. Package com. cmcc. demo;
  26.  
  27.  
  28. Interface ITaskCallback {
  29.  
  30. Void actionreceivmed (int actionId );
  31.  
  32. }
  33.  
  34. Note: theoretically, the parameter can pass the basic data type and String, and the Bundle derived class. However, in Eclipse, the current ADT does not support Bundle as the parameter. It is said that Ant compilation is acceptable, I did not try.
  35.  
  36. 2. Compile the AIDL file. If Ant is used, it may need to be manually. If Eclipse plugin is used, java files can be automatically generated and compiled based on the adil file without human intervention.
  37.  
  38. 3. In the Java file, implement the interface defined in AIDL. the compiler will generate a JAVA Interface Based on the AIDL interface. This interface has an internal abstract class named Stub, which inherits several methods required to extend the interface and implement remote calls. Next we need to implement several custom interfaces.
  39.  
  40. Implementation of interfaces in ITaskBinder. aidl:
  41.  
  42. Private final ITaskBinder. Stub mBinder = new ITaskBinder. Stub (){
  43.  
  44. Public void stopRunningTask (){
  45.  
  46. // @ TODO
  47.  
  48. }
  49.  
  50.  
  51. Public boolean isTaskRunning (){
  52.  
  53. // @ TODO
  54.  
  55. Return false;
  56.  
  57. }
  58.  
  59.  
  60. Public void registerCallback (ITaskCallback cb ){
  61.  
  62. If (cb! = Null) mCallbacks. register (cb );
  63.  
  64. }
  65.  
  66. Public void unregisterCallback (ITaskCallback cb ){
  67.  
  68. If (cb! = Null) mCallbacks. unregister (cb );
  69.  
  70. }
  71.  
  72. };
  73.  
  74. In MyActivity. java, implement the ITaskCallback. aidl interface:
  75.  
  76. Private ITaskCallback mCallback = new ITaskCallback. Stub (){
  77.  
  78. Public void actionreceivmed (int id ){
  79.  
  80. // TODO
  81.  
  82. Printf ("callback id =" + id );
  83.  
  84. }
  85.  
  86. };

4. Provide the AndRoid interface ITaskBinder to the client. If a service is written, extend the Service and reload the onBind () method to return an instance of the class that implements the preceding interface. The returned mBinder is the one defined in the embedded above (MyService. java)

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.