Android的联通性---会话发起协议(SIP)(二)

来源:互联网 发布:中国统计 城市数据分析 编辑:程序博客网 时间:2024/04/30 16:38

创建清单

如果你正在开发一个使用SIP API的应用程序,那么一定要记住,该功能只在Android2.3(API Level 9)以后的版本上才被支持。还有,不是所有的运行Android2.3(API Level 9)以后版本的设备都提供对SIP的支持。

把下列权限添加到你的应用程序清单中,以便使用SIP

1. android.permission.USE_SIP

2. android.permission.INTERNET

3. 要确保你的应用程序只能被安装子提供SIP能力的设备上,就要在你的应用程序清单中添加<uses-sdk android:minSdkVersion=”9”>。这个声明指明你的应用程序要求Android2.3以上的版本。更多的信息请看API级别<uses-sdk>元素相关的文档。

4. 要控制你的应用程序能够被那些不支持SIP的设备过滤掉(例如,在Google Play上),还要把下列声明添加到你的应用程序的清单中:

<uses-feature android:name=”android.hardware.sip.voip” />。这就声明了你的应用程序所使用的SIP API。这个声明应该包含一个android:required数据,用它来指示你是否希望你的应用程序被不提供SIP支持的设备过滤掉。其他的<uses-feature>声明也可能需要,这依赖与你的实现。更多的信息请看<uses-feature>元素的文档。

5. 如果你的应用程序被设计成要接收呼叫,你还必须在应用程序的清单中定义一个接收器(BroadcastReceiver类的子类)。

<receiver android:name=”.IncomingCallReceiver” android:label=”Call Receiver” />

以下是SipDemo示例清单中的摘要:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

         package="com.example.android.sip">

 ...

    <receiver android:name=".IncomingCallReceiver" android:label="Call Receiver"/>

 ...

 <uses-sdk android:minSdkVersion="9" />

 <uses-permission android:name="android.permission.USE_SIP" />

 <uses-permission android:name="android.permission.INTERNET" />

 ...

 <uses-feature android:name="android.hardware.sip.voip" android:required="true" />

 <uses-feature android:name="android.hardware.wifi" android:required="true" />

 <uses-feature android:name="android.hardware.microphone" android:required="true" />

</manifest>

创建SipManager

要使用SIP API,你的应用程序就必须创建一个SipManager对象,在你的应用程序中,该对象负责以下工作:

1. 启动SIP会话;

2. 启动并接受呼叫;

3. 注册和注销SIP服务提供商;

4. 验证会话的联通性。

用下列方式实例化一个新的SipManager对象:

public SipManager mSipManager = null;

if(mSipManager == null) {

mSipManager = SipManager.newInstance(this);

}

注册SIP服务

典型的Android SIP应用程序要涉及到一个或多个用户,每个用户都要有一个SIP账号。在一个Android SIP应用程序中,每个SIP账号由一个SipProfile对象来代表。

一个SipProfile对象定义了一个SIP配置,包括:一个SIP账号、域名和服务器信息。跟运行应用程序的设备上的SIP账号相关联的配置(profile)被叫做本地配置(local profile)。用于连接会话的配置被叫做对等配置(peer profile)。当你的SIP应用程序使用本地的SipProfile对象登录到SIP服务器时,这实际上是用注册设备所用的SIP地址来发送SIP呼叫。

本段向你介绍如何创建SipProfile对象,注册SIP服务器、跟中注册事件。

以下是创建SipProfile对象的代码:

public SipProfile mSipProfile = null;

...

SipProfile.Builder builder = new SipProfile.Builder(username, domain);

builder.setPassword(password);

mSipProfile = builder.build();

以下代码判断会打开本地配置用于拨打电话和(或)接收通用的SIP呼叫。主叫方通过mSipManager.makeAudioCall()方法可以进行后续的呼叫。这段代码还设置了android.SipDemo.INCOMING_CALL操作,该操作会在设备接收到一个呼叫时,由Intent过滤器来使用,以下是注册的步骤:

Intent intent = new Intent();

intent.setAction("android.SipDemo.INCOMING_CALL");

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, Intent.FILL_IN_DATA);

mSipManager.open(mSipProfile, pendingIntent, null);

最后,这段代码把SipRegistrationListener监听器设置到SipManager对象上。这样就跟踪SipProfile对象是否成功的注册到了你SIP服务提供商那儿:

mSipManager.setRegistrationListener(mSipProfile.getUriString(), new SipRegistrationListener() {

public void onRegistering(String localProfileUri) {

   updateStatus("Registering with SIP Server...");

}

public void onRegistrationDone(String localProfileUri, long expiryTime) {

   updateStatus("Ready");

}

public void onRegistrationFailed(String localProfileUri, int errorCode,

   String errorMessage) {

   updateStatus("Registration failed.  Please check settings.");

}

当你的应用程序完成对一个配置的使用时,它应该被关闭,以便释放跟这些对象关联的内存,并从服务器上注销该设备的连接,如:

public void closeLocalProfile() {

   if (mSipManager == null) {

      return;

   }

   try {

      if (mSipProfile != null) {

         mSipManager.close(mSipProfile.getUriString());

      }

    } catch (Exception ee) {

      Log.d("WalkieTalkieActivity/onDestroy", "Failed to close local profile.", ee);

    }

}