您的位置:首页 > 大数据 > 人工智能

AIDL入门

2014-05-05 00:00 701 查看

1.用途

Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信。

为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了RPC方式来实现。与很多其他的基于RPC的解决方案一样,Android使用一种接口定义语言IDL来公开服务的接口。我们知道4个Android应用程序组件中的3个(Activity、BroadcastReceiver和ContentProvider)都可以进行跨进程访问,另外一个Android应用程序组件Service同样可以。因此,可以将这种可以跨进程访问的服务称为AIDL服务。

2.名词

AIDL(Android Interface Definition Language):Android接口定义语言

IDL(Interface Definition Language):接口定义语言

RPC(Remote Procedure Call):远程过程调用

IPC(Inter-Process Communication):进程间通信

3.使用AIDL步骤

1.Create the .aidl file

This file defines the programming interface with method signatures.

2.Implement the interface

The Android SDK tools generate an interface in the Java programming language, based on your .aidl file. This interface has an inner abstract class named Stub that extends Binder and implements methods from your AIDL interface. You must extend the Stub class and implement the methods.

3.Expose the interface to clients

Implement a Service and override onBind() to return your implementation of the Stub class.

来源: <http://developer.android.com/guide/components/aidl.html#Defining>

4.一个栗子

有两个应用:AidlServer和AidlClient.

AidlServer提供一个Add方法



int Add(int a,int b)
{
return a+b;
}

而AidlClient中会通过AIDL调用此方法计算5+9的答案.

核心代码:

1. MyAidl.aidl(AidlServer)



package com.example.aidlserver;
interface MyAidl
{
int Add(int a,int b);
}

2. MyServer.java(AidlServer)



package com.example.aidlserver;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
public class MyServer extends Service
{
public class MyServerImpl extends com.example.aidlserver.MyAidl.Stub
{
@Override
public int Add(int a, int b) throws RemoteException
{
return a+b;
}
}
@Override
public IBinder onBind(Intent intent)
{
return new MyServerImpl();
}
@Override
public void onCreate()
{
super.onCreate();
}
}

3. MainActivity.java(AidlClient)



package com.example.aidlclient;
import com.example.aidlserver.MyAidl;
import android.os.Bundle;
import android.os.IBinder;
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
private MyAidl mMyAidl;
private TextView tv;
private Button bt;
private ServiceConnection serviceConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service)
{
mMyAidl=MyAidl.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name)
{
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button)findViewById(R.id.bt);
tv=(TextView)findViewById(R.id.tv);
bt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
int a=5;
int b=9;
int r=0;
tv.setText(mMyAidl.Add(a, b));
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
bindService(new Intent("com.example.aidlserver.MyAidl"), serviceConnection , Context.BIND_AUTO_CREATE);
}
}

5.源码

AidlServer和AidlClient的源码上传网盘地址:http://pan.baidu.com/s/1c0xgci0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android aidl