AIDL:Android的接口定义语言,接口定义语言,顾名思义,就是定义接口的语言,即利用AIDL可以定义接口。
AIDL简单地说就是进程间通信的方法,类似于Java的的中的RMI,主要用来进程之间通信的;
1.把远程服务的方法抽取成一个单独的接口的Java的文件
2.把接口的Java的文件的后缀名改成AIDL
3.在自动生成的publicbusiness.java文件中,有一个静态类抽象存根,它已经继承了粘合剂类实现了publicbusiness接口,这个类就是新的中间人。
4.把AIDL文件复制粘贴到新的项目中,粘贴的时候AIDL文件所在的包名必须和AIDL创建的时候的包名一致
5.新的项目中,强转中间人对象直接使用Stub.asInterface();
将之前创建的接口文件publicbusiness.java名字修改成publicbusiness.aidl并去掉里面的市民,因为AIDL里面的东西本身就是公开的。

publicbusiness.java是自动生产的,里面实现了中间人的方法:
public static abstract class Stub extends android.os.Binder implements com.ldw.remoteService.publicbusiness
因此在服务中不需要继承和实现一些方法,直接使用存根作为中间人对象。
中间方法定义成了这个
class zhongjian extends Stub{
public void diao(){
beidiaoyong();
}
}
调用这个服务的应用程序需要把AIDL文件也粘贴到代码目录下,注意包名也必须一样,上面创建的publicbusiness.java在com.ldw.remoteService里面,因此调用的时候也要创建一个一样的包把AIDL放进去,里面也会生产一个publicbusiness.java

代码:
APP1:
清单文件
<service android:name="com.ldw.remoteService.remoteService">
<intent-filter >
<action android:name="com.ldw.remote"/>
</intent-filter>
</service>
publicbusiness.aidl
package com.ldw.remoteService;
interface publicbusiness {
void diao();
}
remoteService.java
package com.ldw.remoteService;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import com.ldw.remoteService.publicbusiness.Stub;
public class remoteService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("onBind方法");
return new zhongjian();
}
class zhongjian extends Stub{
public void diao(){
beidiaoyong();
}
}
public void beidiaoyong(){
System.out.println("beidiaoyong方法被调用");
}
}
APP 2:
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调用服务的方法"
android:onClick="diaoyong"
/>
</LinearLayout>
MainActivity.java
package com.ldw.startservice;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import com.ldw.remoteService.publicbusiness;
import com.ldw.remoteService.publicbusiness.Stub;
public class MainActivity extends Activity {
private MyserviceConn conn;
publicbusiness pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn = new MyserviceConn();
}
public void start(View v){
Intent intent = new Intent();
intent.setAction("com.ldw.remote");
startService(intent);
}
public void stop(View v){
Intent intent = new Intent();
intent.setAction("com.ldw.remote");
stopService(intent);
}
public void bind(View v){
Intent intent = new Intent();
intent.setAction("com.ldw.remote");
bindService(intent, conn, BIND_AUTO_CREATE);
}
public void unbind(View v){
unbindService(conn);
}
public void diaoyong(View v){
try {
pb.diao();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class MyserviceConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
//吧中间人对象强转成publicbusiness
pb = Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}
}
Alipay
app1创建一个service,来提供调用
Manifist.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ldw.alipay"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ldw.alipay.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.ldw.alipay.payService">
<intent-filter >
<action android:name="com.ldw.pay"/>
</intent-filter>
</service>
</application>
</manifest>
publicbusiness.aidl
package com.ldw.alipay;
interface publicbusiness {
void pay();
}
MainActivity.java
package com.ldw.alipay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public 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;
}
}
服务payService.java
package com.ldw.alipay;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import com.ldw.alipay.publicbusiness.Stub;
public class payService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new zhongjian();
}
class zhongjian extends Stub{
@Override
public void pay() throws RemoteException {
// TODO Auto-generated method stub
//调用服务的pay方法
payService.this.pay();
}
}
public void pay(){
System.out.println("pay支付");
}
}
app2调用服务中的方法MainActivity.java
package com.ldw.usepay;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import com.ldw.alipay.publicbusiness;
import com.ldw.alipay.publicbusiness.Stub;
public class MainActivity extends Activity {
publicbusiness pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
intent.setAction("com.ldw.pay");
//这次用匿名的内部类来实现
bindService(intent, new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
pb = Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
}
}, BIND_AUTO_CREATE);
}
//调用远程服务的支付方法
public void click(View v){
try {
pb.pay();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本文详细介绍了如何使用Android接口定义语言(AIDL)实现跨进程通信(IPC),包括创建AIDL接口文件、服务端与客户端的交互流程及示例代码。
1042

被折叠的 条评论
为什么被折叠?



