android平台基于sip协议的网络电话实现(知识点及核心代码)
2015-03-19 11:19
549 查看
开发前思索印证的几个问题:
核心代码
[/code]
[/code]
[/code]
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]
相关文章推荐
- 基于visual c++之windows核心编程代码分析(47)实现交换网络的QQ号嗅探
- 基于visual c++之windows核心编程代码分析(59)实现网络简单代理编程
- 基于visual c++之windows核心编程代码分析(59)实现网络简单代理编程
- 基于visual c++之windows核心编程代码分析(47)实现交换网络的QQ号嗅探
- 一个二维码实现IOS和android两个平台的下载核心代码
- 基于TcpDump和pcap文件分析的Android平台网络抓包程序设计与实现【随便】
- 基于visual c++之windows核心编程代码分析(47)实现交换网络的QQ号嗅探
- 基于Android移动平台僵尸网络模拟工具设计与实现
- 教程:实现Android的不同精度的定位(基于网络和GPS)
- 基于SIP协议的IP电话系统设计与实现
- 【转】在android平台上实现基于xmpp的摄像头截屏发送
- 基于rtsp的手机视频点播实现和研究(扩展支持android, ios平台)
- 基于visual c++之windows核心编程代码分析(28)实现开机自启动
- C#基于.Net平台应用程序唯一运行实例C#代码实现
- 教程:实现Android的不同精度的定位(基于网络和GPS)
- 实现Android的不同精度的定位(基于网络和GPS)
- 基于visual c++之windows核心编程代码分析(26)实现文件关联
- 教程:实现Android的不同精度的定位(基于网络和GPS)
- 平台+插件软件设计思想基于COM原型实现的代码剖析
- javascript实现的基于金山词霸网络翻译的代码