【转载自】http://blog.youkuaiyun.com/jindegegesun/article/details/8673514
CSipSimple是运行在android设备上的一个开源的sip协议应用程序,本文其中的拨打电话机制进行大致分析。
项目中,拨打电话利用了AIDL方法来实现。aidl是 Android Interface definition language的缩写,它是一种android内部进程通信接口的描述语言,通过它来定义进程间的通信接口,完成IPC(Inter-Process Communication,进程间通信)。
创建.aidl文件
ISipService.aidl内容如下:
- /**
- * Copyright (C) 2010-2012 Regis Montoya (aka r3gis - www.r3gis.fr)
- * This file is part of CSipSimple.
- *
- * CSipSimple is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- * If you own a pjsip commercial license you can also redistribute it
- * and/or modify it under the terms of the GNU Lesser General Public License
- * as an android library.
- *
- * CSipSimple is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with CSipSimple. If not, see <http://www.gnu.org/licenses/>.
- *
- * This file and this file only is also released under Apache license as an API file
- */
- package com.csipsimple.api;
- import com.csipsimple.api.SipProfileState;
- import com.csipsimple.api.SipCallSession;
- import com.csipsimple.api.MediaState;
- interface ISipService{
- /**
- * Get the current API version
- * @return version number. 1000 x major version + minor version
- * Each major version must be compatible with all versions of the same major version
- */
- .........
- void makeCallWithOptions(in String callee, int accountId, in Bundle options);
- }
方法的接口ISipService。
自动编译生成java文件
eclipse中的ADT插件会自动在aidl文件中声明的包名目录下生成java文件,如下图所示:

ISipService.java
- package com.csipsimple.api;
- public interface ISipService extends android.os.IInterface
- {
- ……
- //Place a call
- ublic void makeCallWithOptions(java.lang.String callee, int accountId, android.os.Bundle options) throws android.os.RemoteException;
- }
IPC实现
项目中拨打电话
void com.csipsimple.api.ISipService.makeCallWithOptions(String msg, String toNumber, long accountId)
结合代码一层层看调用
目录:src\com\csipsimple\ui\dialpad
DialerFragment.java
- private ISipService service;
- private ServiceConnection connection = new ServiceConnection() {
- @Override
- public void onServiceConnected(ComponentName arg0, IBinder arg1) {
- service = ISipService.Stub.asInterface(arg1);
- ........
- }
- @Override
- public void onServiceDisconnected(ComponentName arg0) {
- service = null;
- }
- };
- <span style="color:#333333;"> @Override
- public void placeCall() {
- placeCallWithOption(null);
- }
- private void placeCallWithOption(Bundle b) {
- if (service == null) {
- return;
- }
- String toCall = "";
- Long accountToUse = SipProfile.INVALID_ID;