`
emowuyi
  • 浏览: 1475394 次
文章分类
社区版块
存档分类
最新评论

Android的接口定义语言(aidl)

 
阅读更多

AIDL与其他的接口定义语言很相似。它允许你定义程序接口,用它来在进程间进行交流。在Android系统中,进程之间是相互独立的。它们需要将它们的对象解释成操作系统理解的基本体。这个处理过程是复杂的,所有Android通过AIDL为你处理。

如果你想让你的不同的应用程序客户端接入到服务器来进行IPC(interprocess communication )的话,使用AIDL是十分必要的。在服务中的对线程处理也同样是必要的。

在你设计你的AIDL之前,你应该知道,AIDL调用是直接的调用。你不应该让线程的调用发生。

定义AIDL接口:

你可以使用java的语法来定义该文件,但文件名不需是.aidl,保存在src/目录下。

1.创建.aidl文件

package com.example.android;


interface IRemoteService {
int getPid();

void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}

2.实现接口

private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid(){
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
// Does nothing
}
};

3.暴露接口给客户端

public class RemoteService extends Service {
@Override
public void onCreate() {
super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
// Return the interface
return mBinder;
}

private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {
public int getPid(){
return Process.myPid();
}
public void basicTypes(int anInt, long aLong, boolean aBoolean,
float aFloat, double aDouble, String aString) {
// Does nothing
}
};
}

接入接口:

IRemoteService mIRemoteService;
private ServiceConnection mConnection = new ServiceConnection() {
// Called when the connection with the service is established
public void onServiceConnected(ComponentName className, IBinder service) {
// Following the example above for an AIDL interface,
// this gets an instance of the IRemoteInterface, which we can use to call on the service
mIRemoteService
= IRemoteService.Stub.asInterface(service);
}

// Called when the connection with the service disconnects unexpectedly
public void onServiceDisconnected(ComponentName className) {
Log.e(TAG, "Service has unexpectedly disconnected");
mIRemoteService
= null;
}
};

通过IPC传递对象:

1.你必须实现Parcelable接口。

import android.os.Parcel;
import android.os.Parcelable;

public final class Rect implements Parcelable {
public int left;
public int top;
public int right;
public int bottom;

public static final Parcelable.Creator<Rect> CREATOR = new
Parcelable.Creator<Rect>() {
public Rect createFromParcel(Parcel in) {
return new Rect(in);
}

public Rect[] newArray(int size) {
return new Rect[size];
}
};

public Rect() {
}

private Rect(Parcel in) {
readFromParcel
(in);
}

public void writeToParcel(Parcel out) {
out.writeInt(left);
out.writeInt(top);
out.writeInt(right);
out.writeInt(bottom);
}

public void readFromParcel(Parcel in) {
left
= in.readInt();
top
= in.readInt();
right
= in.readInt();
bottom
= in.readInt();
}

2.调用IPC方法

public static class Binding extends Activity {

IRemoteService mService = null;

ISecondary mSecondaryService = null;

Button mKillButton;
TextView mCallbackText;

private boolean mIsBound;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.remote_service_binding);

// Watch for button clicks.
Button button = (Button)findViewById(R.id.bind);
button.setOnClickListener(mBindListener);
button = (Button)findViewById(R.id.unbind);
button.setOnClickListener(mUnbindListener);
mKillButton = (Button)findViewById(R.id.kill);
mKillButton.setOnClickListener(mKillListener);
mKillButton.setEnabled(false);

mCallbackText = (TextView)findViewById(R.id.callback);
mCallbackText.setText("Not attached.");
}

/**
* Class for interacting with the main interface of the service.
*/
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {

mService = IRemoteService.Stub.asInterface(service);
mKillButton.setEnabled(true);
mCallbackText.setText("Attached.");

// We want to monitor the service for as long as we are
// connected to it.
try {
mService.registerCallback(mCallback);
} catch (RemoteException e) {
}

// As part of the sample, tell the user what happened.
Toast.makeText(Binding.this, R.string.remote_service_connected,
Toast.LENGTH_SHORT).show();
}

public void onServiceDisconnected(ComponentName className) {
// This is called when the connection with the service has been
// unexpectedly disconnected -- that is, its process crashed.
mService = null;
mKillButton.setEnabled(false);
mCallbackText.setText("Disconnected.");

// As part of the sample, tell the user what happened.
Toast.makeText(Binding.this, R.string.remote_service_disconnected,
Toast.LENGTH_SHORT).show();
}
};

private ServiceConnection mSecondaryConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder service) {

mSecondaryService = ISecondary.Stub.asInterface(service);
mKillButton.setEnabled(true);
}

public void onServiceDisconnected(ComponentName className) {
mSecondaryService = null;
mKillButton.setEnabled(false);
}
};

private OnClickListener mBindListener = new OnClickListener() {
public void onClick(View v) {

bindService(new Intent(IRemoteService.class.getName()),
mConnection, Context.BIND_AUTO_CREATE);
bindService(new Intent(ISecondary.class.getName()),
mSecondaryConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
mCallbackText.setText("Binding.");
}
};

private OnClickListener mUnbindListener = new OnClickListener() {
public void onClick(View v) {
if (mIsBound) {

if (mService != null) {
try {
mService.unregisterCallback(mCallback);
} catch (RemoteException e) {

}
}

// Detach our existing connection.
unbindService(mConnection);
unbindService(mSecondaryConnection);
mKillButton.setEnabled(false);
mIsBound = false;
mCallbackText.setText("Unbinding.");
}
}
};

private OnClickListener mKillListener = new OnClickListener() {
public void onClick(View v) {

if (mSecondaryService != null) {
try {
int pid = mSecondaryService.getPid();

Process.killProcess(pid);
mCallbackText.setText("Killed service process.");
} catch (RemoteException ex) {

Toast.makeText(Binding.this,
R.string.remote_call_failed,
Toast.LENGTH_SHORT).show();
}
}
}
};


private IRemoteServiceCallback mCallback = new IRemoteServiceCallback.Stub() {

public void valueChanged(int value) {
mHandler.sendMessage(mHandler.obtainMessage(BUMP_MSG, value, 0));
}
};

private static final int BUMP_MSG = 1;

private Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
switch (msg.what) {
case BUMP_MSG:
mCallbackText.setText("Received from service: " + msg.arg1);
break;
default:
super.handleMessage(msg);
}
}

};
}

分享到:
评论

相关推荐

    Android AIDL接口定义语言

    在Windows系统中存在IPC管道服务、MailSolt邮槽、消息等方法,在Android平台上提供了一种中间层语言 AIDL Android接口定义语言(Android Interface Definition Language)。  实现IPC服务通过使用AIDL步骤主要有: ...

    Android Studio AIDL实现跨进程通信

    AIDL:Android Interface Definition Language,即Android接口定义语言. android studio中使用aidl实现跨进程通讯,具体步骤如下

    Android Studio实现Service AIDL

    为使应用程序之间能够彼此通信,Android提供了IPC (Inter Process Communication,进程间通信)的一种独特实现: AIDL (Android Interface Definition Language, Android接口定义语言)。 建立两个Android项目,...

    AIDL客户端demo

    AIDL(Android 接口定义语言) 是 Android 提供的一种进程间通信 (IPC) 机制。通过这种机制,我们只需要写好 aidl 接口文件,编译时系统会帮我们生成 Binder 接口,实现进程之间的通讯。 一、如何定义aidl文件 1、...

    Android接口定义语言1

    2. 实现接口 (#ImplementTheInterface)3. 向客户端公开该接口 (#ExposeTheInterface)1. 创建 .aidl

    android项目之aidl跨进程调用举例Demo

    3. 实现接口-AIDL编译器从AIDL接口文件中利用Java语言创建接口,该接口有一个继承的命名为Stub的内部抽象类(并且实现了一些IPC调用的附加方法),要做的就是创建一个继承YourInterface.Stub的类并且实现在.aidl文件...

    AIDL最简单的使用步骤

    AIDL:Android Interface Definition Language,即Android接口定义语言。 为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现。与很多其他...

    Android 进程间通信AIDL使用详解

    AIDL 意思即 Android Interface Definition Language,翻译过来就是Android接口定义语言,是用于定义服务器和客户端通信接口的一种描述语言,可以拿来生成用于IPC的代码。从某种意义上说AIDL其实是一个模板,因为在...

    Android-AIDL文件实现两个工程之间互相传输

    AIDL概述:AIDL是一个缩写,全称是Android Interface Definition Language,也就是Android接口定义语言,设计这门语言的目的是为了实现进程间通信。接下来我写了两个demo(AildeService和AidleClient),他们之间...

    AIDL.rar示例

    aidl(Android 接口定义语言 )

    Android AIDL入门测试案例

    AIDL(Android接口定义语言),目的是为了实现进程间通信,尤其是在涉及多进程并发情况下的进程间通信。 博客地址:http://blog.csdn.net/chenzheng8975/article/details/54140098

    Android AIDL使用详解

    aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口 icp:interprocess communication :内部进程通信

    Android程序设计之AIDL实例详解

    AIDL(AndRoid接口描述语言)是一种借口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象...

    Android 使用AIDL进行两个APP之间通讯以及相互消息回调(一)

    AIDL:Android Interface Definition Language,翻译过来就是Android接口定义语言。是用于定义服务器和客户端通信接口的一种描述语言,可以拿来生成用于IPC的代码。所以使用AIDL需要一个服务端和客户端 作用:可以在...

    Android AIDL示例

    Android AIDL简单的演示用例 Android的 AIDL是安卓接口定义语言,是Android跨进程通信的一种常用方式。

    Android中两个APP之间的AIDL调用测试.rar

    ①Android Interface definition language(aidl,android接口定义语言),其目的实现跨进程的调用。进程是程序在os中执行的载体,一个程序对应一个进程,不同进程就是指不同程序,aidl实现不同程序之间的调用。 ②...

    AIDL示例(Android Interface Definition Language)

    与很多其他的基于RPC的解决方案一样,Android使用一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口。我们知道4个Android应用程序组件中的3个(Activity、BroadcastReceiver和...

    AIDL服务端

    AIDL(Android 接口定义语言) 是 Android 提供的一种进程间通信 (IPC) 机制。通过这种机制,我们只需要写好 aidl 接口文件,编译时系统会帮我们生成 Binder 接口,实现进程之间的通讯。 一、如何定义aidl文件 1、...

    Android AIDL实现跨进程通信的示例代码

    AIDL是Android接口定义语言,它可以用于让某个Service与多个应用程序组件之间进行跨进程通信,从而可以实现多个应用程序共享同一个Service的功能。 实现步骤 例:用 A程序去访问 B程序的MyService.java服务 在B中...

    AIDL进程间通讯demo

    AIDL进程间通讯demo,A [android] I [Interface] D [Definition] L [Language],Android接口定义语言。 作用:方便系统为我们生成代码从而实现跨进程通讯,仅此而已。(玉刚老师如是说也),也就是说这个AIDL就只是一...

Global site tag (gtag.js) - Google Analytics