您的位置:首页 > 理论基础 > 计算机网络

android平台基于sip协议的网络电话实现(知识点及核心代码)

2015-03-19 11:19 549 查看
开发前思索印证的几个问题:
1)关于broadcastreceiver,在静态和动态两种注册方法中若同时使用,会触发两次onReceive()方法。

2)无论在service或activity中,只有动态绑定broadcastreceiver且service(或activity)实例化情况下才能在onReceive()方法中使用SipService service = (SipService) context;(这不是废话)

3)SipAudioCall.Listener()的 onRinging(SipAudioCall call, SipProfile caller)方法中的call是呼叫方;onReceive(Context context, Intent intent)中intent是本地SipManager中的intent,具体过程是SipManager收到呼叫请求发送本地挂起的intent触发broadcastreceiver

核心代码
以呼叫接收为例,大致有3个步骤

1)注册manager(sip连接管理器)profile(账户本地配置)

public void initializeManager() {
if (manager == null) {
manager = SipManager.newInstance(this);
}

initializeLocalProfile();
}

public void initializeLocalProfile() {
if (manager == null) {
return;
}

if (me != null) {
closeLocalProfile();
}

Context ct = SipService.this;
SharedPreferences prefs = ct.getSharedPreferences("Setting",
MODE_PRIVATE);
String username = prefs.getString("namePref", "");
String domain = prefs.getString("domainPref", "");
String password = prefs.getString("passPref", "");

if (username.length() == 0 || domain.length() == 0
|| password.length() == 0) {
Log.i("Info", "账户配置为空");
return;
}

try {
SipProfile.Builder builder = new SipProfile.Builder(username,
domain);
builder.setPassword(password);
me = builder.build();

Intent i = new Intent();
i.setAction("android.kyee.INCOMING_CALL");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i,
Intent.FILL_IN_DATA);
manager.open(me, pi, null);

// This listener must be added AFTER manager.open is called,
// Otherwise the methods aren't guaranteed to fire.

manager.setRegistrationListener(me.getUriString(),
new SipRegistrationListener() {
public void onRegistering(String localProfileUri) {
statusText = "注册中......";
Intent intent = new Intent();
intent.setAction("android.sipservice.CHANGE_STATUS");
sendBroadcast(intent);
}

public void onRegistrationDone(String localProfileUri,
long expiryTime) {
statusText = "已注册";
Intent intent = new Intent();
intent.setAction("android.sipservice.CHANGE_STATUS");
sendBroadcast(intent);
}

public void onRegistrationFailed(
String localProfileUri, int errorCode,
String errorMessage) {
statusText = "注册失败";
Intent intent = new Intent();
intent.setAction("android.sipservice.CHANGE_STATUS");
sendBroadcast(intent);
}
});
} catch (ParseException pe) {
Log.i("Info", "ParseException");
} catch (SipException se) {
Log.i("Info", "SipException");
}
}


[/code]
2)动态注册broadcastreceiver及过滤器

IntentFilter filter = new IntentFilter();
filter.addAction("android.kyee.INCOMING_CALL");
IncomingCallReceiver receiver = new IncomingCallReceiver();
registerReceiver(receiver, filter);

[/code]
3)实现broadcastreceiver类及onReceive()方法

SipAudioCall incomingCall = null;
try {

SipAudioCall.Listener listener = new SipAudioCall.Listener() {
@Override
public void onRinging(SipAudioCall call, SipProfile caller) {
try {
call.answerCall(30);
} catch (Exception e) {
e.printStackTrace();
}
}
};
SipService service = (SipService) context;//获取context上写文环境用于调用其方法(前面提到该上下文必须已实例化)
incomingCall = service.manager.takeAudioCall(intent, listener);//用takeAudioCall方法取得连接
incomingCall.answerCall(30);//建立连接,这里连接之后的通话放在另一个activity中处理
// wtActivity.showDialog(1);
// incomingCall.startAudio();
// incomingCall.setSpeakerMode(true);
// if (incomingCall.isMuted()) {
// incomingCall.toggleMute();
// }

service.call = incomingCall;

} catch (Exception e) {

if (incomingCall != null) {
incomingCall.close();
}
}
Intent OneIntent = new Intent();
OneIntent.setClass(context, CallActivity.class);
OneIntent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(OneIntent);

}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐