AIDL入门

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

AIDL入门

时间: 2024-08-13 17:30:05

AIDL入门的相关文章

Android进程通信:AIDL入门实例

AIDL即 Android Interface Definition Language.原因:On Android, one process cannot normally access thememory of another process. 也就是说AIDL用于android进程间通信,下面就记录一下第一个aidl的demo. 官方文档也给出了基本的使用方法,如下图: 1. 在android项目的src相关包下创建一个aidl文件 IRemoteService.aidl: package

android入门教程之AIDL简单入门

最近即将做的一个项目里面需要使用到AIDL(Android Interface Definition Language:接口描述语言)技术,就在麦子学院android开发视频教程上简单瞄了一眼,我的理解它和java中的RMI的概念差不多,于是就自己尝试的测试了下.废话不说了,直接开始  1.既然AIDL是既然是可以在不同进程间进行操作,那么我们首先就需要准备两个项目,我们先来看看不同的两个项目结构式什么样子的.如图:   我们先暂且定义一个项目为"服务器端项目",一个为"客户

入门篇:10.Android中AIDL(安卓接口定义语言)跨应用操作Service

Android系统中的进程之间不能共享内存,因此,需要提供一些机制在不同进程之间进行数据通信. 为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Call,RPC)方式来实现.与很多其他的基于RPC的解决方案一样,Android使用一种接口定义语言(Interface Definition Language,IDL)来公开服务的接口.我们知道4个Android应用程序组件中的3个(Activity.BroadcastRec

Android基础入门教程——4.2.3 Service精通

Android基础入门教程--4.2.3 Service精通 标签(空格分隔): Android基础入门教程 本节引言: 本节,我们继续来研究Service(服务)组件,本节将会学习下Android中的AIDL跨进程通信的一些 概念,并不深入到源码层次,暂时知道是什么,会用即可!开始本节内容~ 本节对应官方文档:Binder 1.Binder机制初涉 1)IBinder和Binder是什么鬼? 我们来看看官方文档怎么说: 中文翻译: IBinder是远程对象的基本接口,是饿了高性能而设计的轻量级

[书目20160624]Android应用开发从入门到精通

卢海东 著 第1章 揭开神秘面纱——Android系统简介 1   1.1 认识Android系统 2   1.1.1 Android成长历程 2   1.1.2 发行版本 3   1.1.3 得到大家的认可——Android系统的市场份额 3   1.2 Android架构解析 4   1.2.1 Android系统架构图 4   1.2.2 应用程序(Applications) 5   1.2.3 应用程序框架层(Framework) 6   1.2.4 系统运行库(Libraries) 7

Android基础入门教程——10.1 TelephonyManager(电话管理器)

Android基础入门教程--10.1 TelephonyManager(电话管理器) 标签(空格分隔): Android基础入门教程 本节引言: 本章节是Android基础入门教程的最后一章,主要讲解是一些零零散散的一些知识点,以及一些遗漏 知识点的补充,这些零散的知识点包括,各种系统服务的使用,比如本节的电话管理器,短信管理器, 振动器,闹钟,壁纸等等,还有传感器之类的东西!乱七八糟什么都有哈!好的,本节我们要学习的 是TelephonyManager,见名知义:用于管理手机通话状态,获取电

Android基础入门教程——4.2.2 Service进阶

Android基础入门教程--4.2.2 Service进阶 标签(空格分隔): Android基础入门教程 本节引言 上节我们学习了Service的生命周期,以及两种启动Service的两种方法, 本节继续来深入了解Service中的IntentService,Service的使用实例: 前台服务与轮询的实现! 1.IntentService的使用 在上一节后我们已经知道了如何去定义和启动Service,但是如果我们直接把 耗时线程放到Service中的onStart()方法中,虽然可以这样做

【Android的从零单排开发日记】之入门篇(五)——Android四大组件之Service

这几天忙着驾校考试,连电脑都碰不到了,今天总算告一段落了~~Service作为Android的服务组件,默默地在后台为整个程序服务,辅助应用与系统中的其他组件或系统服务进行沟通.它跟Activity的级别差不多,但不能自己运行只能后台运行.service可以在很多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity这个时候程序要在后台继续播放,比如检测SD卡上文件的变化,再或者在后台记录你地理信息位置的改变等等, 总之服务总是藏在后台的. ps:Service运行在主线程中的,所

小猪的Android入门之路 Day 9 part 2

小猪的Android入门之路 Day 9 part 2 Android四大组件之--AIDL实现跨进程通信 --转账请注明出处:coder-pig 本节引言: 在上一part中我们对Service进行了简单的学习: 什么是Service,Service的生命周期,StartService和BindService的区别以及使用 IntentService来解决Service的异步问题; 而在今天的这一Part中将会研究另一个东西:IPC,安卓给我们提供了AIDL Service 来完成进程间的数据