天天看点

Android AIDL的使用方法AIDL的使用,AIDL是Android Interface Definiton Language的缩写,用于Android内部进程间通信的一种机制

AIDL的使用,AIDL是Android Interface Definiton Language的缩写,用于Android内部进程间通信的一种机制

1、定义AIDL文件,产生AIDL接口对象。这个文件服务端(被绑定端)和客服端(主动绑定端)都要加入该文件。

package com.xhc.bc.xgc;

interface Talk {

void sendJsonString(String string,String toName); 

void recvJsonString(String string); 

String getDoorIpAndSip(String commandType);

}

2、创建服务文件,定义Action

<service

android:name="com.xhc.pjsip.service.XmppService"

android:enabled="true" >

<intent-filter>

<action android:name="xhc.xmpp.result.toTalk" />

<category android:name="android.intent.category.DEFAULT" ></category>

</intent-filter>

</service>

3、客服端绑定服务Action,使用接口对象方法。如果做客服端就不需要上面的创建服务就只要知道Action是什么。

private static Boolean register() {

return mContext.bindService(new Intent("xhc.get.talk.command.to.xmpp"),mServiceConnection,1);

}

//获得对象

static ServiceConnection mServiceConnection = new ServiceConnection() {

@Override

public void onServiceConnected(ComponentName name, IBinder service) {

talk = Talk.Stub.asInterface(service);

if(talk != null){

try{

for (String ss : commands) {

talk.sendJsonString(ss, CommandEnum.talk.toString());

}

commands.clear();

}catch(Exception e){

}

}else{

LogUtils.LogTalk( LogCls + "talk==null");

}

}

@Override

public void onServiceDisconnected(ComponentName name) { // TODO

register();

}

};

//使用接口

talk.sendJsonString(ss, CommandEnum.talk.toString());

4、服务端实现接口定义方法。第2和第4步是在服务端要做的工作。

Talk.Stub mall=new Talk.Stub() {

//接口方法一

...........

//;接口方法二

        ...........

}

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return mall;

}