Analysis of AIDL implementation principles in Android Development (source code sharing)

Source: Internet
Author: User

Before starting this chapter, I would like to introduce a concept of COM ------- Proxy/Stub structure (Proxy/Stub structure)

Examples/examples + examples/eo6y + examples + Mihv + 67 + qOsyKG/7rv6uPm + 3bf + samples/g06bK/cG/samples + ouvOjrLLZ1/fN6rPJoaPIob/uu/samples/ 7O8cb3way907XEo6zL + records + 67 + tPrtOa4 + c2o0MWjrLf + zvHG99PrtOa4 + records + b7Nyse3/records + records/records "http://www.2cto.com/uploadfile/Collfiles/20140417/2014041709210782.gif" alt = "\">

Android adds a layer to the traditional C/S architecture to implement IPC. The following describes in detail the aidl implementation principles of android.

AIDL (Android Interface Definition Language) is similar to other IDL you have encountered. It allows you to define the programming interface, client and service to reach an agreement to communicate with each other and use the inter-process communication (IPC ). On Android, a process cannot normally access the memory of another process, and AIDL can be implemented for you. The use of AIDL is very different from the use of Messenger I wrote earlier. We need to build an aidl server first. The following three steps are involved in creating an aidl file, this file defines the method signature programming interface. When you build an aidl file, the Android SDK automatically generates an internal-based interface ., And save the aidl file to the Project Creation/directory. (2) Implement the aidl-based Java programming language interface automatically generated by the interface. Android SDK tool. This interface has an internal abstract class named stub, which extends ibinder from the AIDL interface and implementation method. You must extend the Stub class and implementation methods. (3) Publish the interface to the client. Implement a Service and override onBind () to return the implementation of your stub class.

Server framework

DataService. aidl source code is as follows:

package com.example.service;interface DataService{   int getData(String name);}

Source code of MyService:

package com.example.f25_aidl01;import com.example.service.DataService;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;public class MyService extends Service {@Overridepublic void onCreate() {// TODO Auto-generated method stubsuper.onCreate();}private final DataService.Stub mBinder=new DataService.Stub(){@Overridepublic int getData(String name) throws RemoteException {// TODO Auto-generated method stubif(name.equals("1")){return 1;}return 0;}};@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn mBinder;}}

Before starting the Client, start the Service on the server, and write

 
             
                              
                              
 

The Client must declare the same aidl file as the server.

Source code of MainAcitivity

package com.example.f25_aidl_client;import com.example.service.DataService;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.app.Activity;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.ServiceConnection;import android.util.Log;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private Button button, button2;private DataService dataService;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) this.findViewById(R.id.button1);button2 = (Button) this.findViewById(R.id.button2);button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(DataService.class.getName());bindService(intent, connection, Context.BIND_AUTO_CREATE);}});button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubtry {int a = dataService.getData("1");Toast.makeText(MainActivity.this, "---->"+a, Toast.LENGTH_LONG).show();} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});}ServiceConnection connection = new ServiceConnection() {@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stubLog.i("TAG", "-------->unbind");}@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubdataService = DataService.Stub.asInterface(service);}};@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


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.