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

	}


發佈了33 篇原創文章 · 獲贊 40 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章