我们都知道在Android中的每一个应用是一个进程,其实每一个应用就相当于Linux系统里面的一个用户,进程和进程之间的通信其实就相当于用户和用户之间的通信,为了实现这种跨进程通信,Android提供了AIDL Service ( Android Interface definition language).

与邦等本地Service不同的是,本地Service的onBind()方法会直接把IBinder对象本身传给客户端的ServiceConnection的onServiceConnected方法的第二个参数。但远程Service的onBind()方法只是将IBinder对象的代理传给客户端的ServiceConnection的onService Connected方法的第二个参数。

ALDL定义的特点:

1、AIDL定义接口的源码必须以.aidl结尾。

2、AIDL接口中用到数据类型,除了基本类型、String、List、Map、CharSequence之外,其他类型全部都需要导包,即使它们在同一个包中也需要导包。

下面来具体实现一个示例:

(1)定义AIDL接口(和Java接口非常类似)

package com.example.testservice;

interface ICat {
String getColor();
double getWeight();
}

(2)定以好上面接口后保存,ADT工具会自动在gen.com.example.service目录下生成一个ICat.jar接口,在该接口中包含一个Stub内部类,该内部类实现了IBinder、ICat两个接口,这个Stub类将会作为远程Service的回调类——它实现了IBuilder接口,因此可作为Service的onBind()方法的返回值。这个Stub类就是一个代理类,同样可以实现IBinder的功能,并且还额外实现了ICat远程访问的功能。

自动生成的ICat.java接口

/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: D:\\adt-bundle-windows-x86-20130729\\eclipse\\workspace\\TestService\\src\\com\\example\\testservice\\ICat.aidl
*/
package com.example.testservice; public interface ICat extends android.os.IInterface {
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements
com.example.testservice.ICat {
private static final java.lang.String DESCRIPTOR = "com.example.testservice.ICat"; /** Construct the stub at attach it to the interface. */
public Stub() {
this.attachInterface(this, DESCRIPTOR);
} /**
* Cast an IBinder object into an com.example.testservice.ICat
* interface, generating a proxy if needed.
*/
public static com.example.testservice.ICat asInterface(
android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.example.testservice.ICat))) {
return ((com.example.testservice.ICat) iin);
}
return new com.example.testservice.ICat.Stub.Proxy(obj);
} @Override
public android.os.IBinder asBinder() {
return this;
} @Override
public boolean onTransact(int code, android.os.Parcel data,
android.os.Parcel reply, int flags)
throws android.os.RemoteException {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getColor: {
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.getColor();
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_getWeight: {
data.enforceInterface(DESCRIPTOR);
double _result = this.getWeight();
reply.writeNoException();
reply.writeDouble(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
} private static class Proxy implements com.example.testservice.ICat {
private android.os.IBinder mRemote; Proxy(android.os.IBinder remote) {
mRemote = remote;
} @Override
public android.os.IBinder asBinder() {
return mRemote;
} public java.lang.String getInterfaceDescriptor() {
return DESCRIPTOR;
} @Override
public java.lang.String getColor()
throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getColor, _data, _reply,
0);
_reply.readException();
_result = _reply.readString();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
} @Override
public double getWeight() throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
double _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getWeight, _data, _reply,
0);
_reply.readException();
_result = _reply.readDouble();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
} static final int TRANSACTION_getColor = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getWeight = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
} public java.lang.String getColor() throws android.os.RemoteException; public double getWeight() throws android.os.RemoteException;
}

(3)定义一个Service实现类,该Service的onBind()方法所返回的IBinder对象应该是ADT所生成的ICat.Stub的子类的实例。这一步的主要作用是将上面创建的接口暴漏给客户端。

package com.example.testservice;

import java.util.Timer;
import java.util.TimerTask; import com.example.testservice.ICat.Stub; import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException; public class AidlService extends Service{ private CatBinder catBinder;
Timer timer = new Timer();
String[] colors = new String[]{
"红色",
"黄色",
"黑色"
};
double[] weights = new double[]{
2.3,
3.1,
1.58
}; private String color;
private double weight; @Override
public IBinder onBind(Intent intent) {
/*
* 返回catBinder对象
* 在绑定本地Service的情况下,该catBinder对象会直接传给客户端的
* ServiceConnection对象的onServiceConnected方法的第二个参数;
* 在绑定远程Service的情况下,只将catBinder对象的代理传给客户端的
* ServiceConnection对象的onServiceConnected方法的第二个参数;
*/
return catBinder;
} //继承Stub,也就是实现了ICat接口,并实现了IBinder接口
public class CatBinder extends Stub{ @Override
public String getColor() throws RemoteException {
// TODO Auto-generated method stub
return color;
} @Override
public double getWeight() throws RemoteException {
// TODO Auto-generated method stub
return weight;
} } @Override
public void onCreate() {
super.onCreate();
catBinder = new CatBinder();
timer.schedule(new TimerTask() { @Override
public void run() {
int rand = (int)(Math.random() * 3);
color = colors[rand];
weight = weights[rand];
System.out.println("---------" + rand);
}
}, 0, 800);
} }

(4)在AndroidMainfest.xml文件中配置该Service

        <service android:name=".AidlService">
<intent-filter>
<action android:name="com.example.testservice.action.AIDL_SERVICE"/>
</intent-filter>
</service>

(5)客户端访问AIDL Service

上面说过AIDL是定义在两个进程之间通信的一个接口,既然是一个通信协议,客户端同样需要前面定义的AIDL接口,所以第一步就是将AIDL文件复制到客户端应用中。

package com.example.testservice;

import android.app.Activity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class AidlClient extends Activity{
private ICat catService;
private Button get;
EditText color, weight;
private ServiceConnection conn = new ServiceConnection() { @Override
public void onServiceDisconnected(ComponentName name) { } @Override
public void onServiceConnected(ComponentName name, IBinder service) {
//获取远程Service的onBind方法返回的对象的代理
catService = ICat.Stub.asInterface(service);
}
}; protected void onCreate(android.os.Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
get = (Button)findViewById(R.id.get);
color = (EditText)findViewById(R.id.color);
weight = (EditText)findViewById(R.id.weight);
//创建所需绑定的Service的Intent
Intent intent = new Intent();
intent.setAction("com.example.testservice.action.AIDL_SERVICE");
bindService(intent, conn, Service.BIND_AUTO_CREATE);
get.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
try {
color.setText(catService.getColor());
weight.setText(catService.getWeight() + "");
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}; @Override
protected void onDestroy() {
super.onDestroy();
//解除绑定
this.unbindService(conn);
}
}

下一篇将介绍如何在进程之间传递复杂数据

Android菜鸟的成长笔记(21)——跨进程调用Service的更多相关文章

  1. Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上)

    原文:[置顶] Android菜鸟的成长笔记(14)—— Android中的状态保存探究(上) 我们在用手机的时候可能会发现,即使应用被放到后台再返回到前台数据依然保留(比如说我们正在玩游戏,突然电话 ...

  2. Android菜鸟的成长笔记(7)——什么是Activity

    原文:[置顶] Android菜鸟的成长笔记(7)——什么是Activity 前面我们做了一个小例子,在分析代码的时候我们提到了Activity,那么什么是Activity呢? Activity是An ...

  3. Android菜鸟的成长笔记(4)——你真的理解了吗?

    原文:Android菜鸟的成长笔记(4)--你真的理解了吗? 在上一篇中我们查看了QQ的apk源文件中的布局结构,并仿照QQ完成了我们第一个应用的界面,详细请看<Android菜鸟的成长笔记&g ...

  4. Android菜鸟的成长笔记(17)—— 再看Android中的Unbounded Service

    原文:Android菜鸟的成长笔记(17)-- 再看Android中的Unbounded Service 前面已经写过关于startService(Unbounded Service)的一篇文章:&l ...

  5. Android菜鸟的成长笔记(3)——给QQ登录界面说So Easy

    原文:Android菜鸟的成长笔记(3)--给QQ登录界面说So Easy 上一篇:Android菜鸟的成长笔记(2)--第一个Android应用 我们前面已经做了第一个Android应用程序,虽然有 ...

  6. Android菜鸟的成长笔记(2)——第一个Android应用

    原文:Android菜鸟的成长笔记(2)--第一个Android应用 上一篇:Android菜鸟的成长笔记(1)--Anddroid环境搭建从入门到精通 在上一篇Android菜鸟的成长笔记(1)中我 ...

  7. Android菜鸟的成长笔记(1)——Android开发环境搭建从入门到精通

    原文:Android菜鸟的成长笔记(1)--Android开发环境搭建从入门到精通 今天在博客中看到好多Android的初学者对Android的开发环境的搭建不熟悉而导致不能进行学习,所以我决定自己写 ...

  8. Android菜鸟的成长笔记(13)——异步任务(Async Task)

    原文:[置顶] Android菜鸟的成长笔记(13)——异步任务(Async Task) Android的UI线程主要负责处理用户的事件及图形显示,因此主线程UI不能阻塞,否则会弹出一个ANR(App ...

  9. Android菜鸟的成长笔记(12)——Handler、Loop、MessageQueue

    原文:[置顶] Android菜鸟的成长笔记(12)——Handler.Loop.MessageQueue 当一个程序第一次启动时,Android会启动一条主线程(Main Thread),主线程主要 ...

  10. Android菜鸟的成长笔记(11)——Android中的事件处理

    原文:[置顶] Android菜鸟的成长笔记(11)——Android中的事件处理 Android提供了两种方式来处理事件,一个是基于回调的事件处理,另一个是基于监听的事件处理,举个例子: 基于回调的 ...

随机推荐

  1. STL_算法_删除(unique、unique_copy)

    C++ Primer 学习中. . . 简单记录下我的学习过程 (代码为主) 全部容器适用 unique(b,e) unique(b,e,p) unique_copy(b1,e1,b2) unique ...

  2. Spring Profiles example--转载

    原文地址:http://www.mkyong.com/spring/spring-profiles-example/ Spring @Profile allow developers to regis ...

  3. Android AutoWrapTextView 解决中英文排版问题

    1.概述 最近项目有新需求,UED给了个卡券密码的UI样式,如图: 我一看很简单啊,一个TextView解决问题,然后做好以后在模拟器里一看..... 纳尼,这个时候才想起来,TextView 中英文 ...

  4. Fragment Summary 2/2

    出处:http://blog.csdn.net/lmj623565791/article/details/37992017 上篇博客中已经介绍了Fragment产生原因,以及一些基本的用法和各种API ...

  5. Altium Designer 在pcb下导入的原件引脚是绿的

    当然也可能是其他规则原因导致的

  6. angular material dialog应用

    1. 打开弹窗的点击事件 project.component.html <button mat-icon-button class="action-button" (clic ...

  7. 最大公约数最小公倍数 (例:HDU2028 Lowest Common Multiple Plus)

    也称欧几里得算法 原理: gcd(a,b)=gcd(b,a mod b) 边界条件为 gcd(a,0)=a; 其中mod 为求余 故辗转相除法可简单的表示为: int gcd(int a, int b ...

  8. unmapping error

    否则,会映射一个Getch的器件,就会报unmapping 的error

  9. cpu信息

    在linux系统下能够通过cat /proc/cpuinfo来查看本机上cpu的相关信息,通过processor能够推断逻辑cpu的个数,physical id能够推断物理cpu的个数,通过cpu c ...

  10. Python代码优化及技巧笔记(一)

    前言 这里是记录一些本人在开发过程中遇到的一些细节问题.与君共勉. 版权说明 著作权归作者全部.商业转载请联系作者获得授权,非商业转载请注明出处. 作者:Coding-Naga链接:http://bl ...