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

开发前思索印证的几个问题:
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");
		}
	}


2)动态注册broadcastreceiver及过滤器
IntentFilter filter = new IntentFilter(); 
		filter.addAction("android.kyee.INCOMING_CALL");
		IncomingCallReceiver receiver = new IncomingCallReceiver();
		registerReceiver(receiver, filter);

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);

	}


  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值