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;}}