使用飞信SDK开发短信收发程序
利用飞信的协议可以在线收发消息,或是向手机发送消息。由此,可以自己来完成一个IM工具。
本文即是对飞信SDK的使用方法,及如何开发作一个说明。
一、引用FetionSDK
飞信是采用C#开发的,所有的程序集均是.NET,因此我们也需要使用Delphi.NET/Chrome来进行相关的开发。在Chrome中,新建一个工程,并引入FetionSDK.dll,当然您也可以使用Delphi2007 for .NET,开发出来结果一样。
其实我曾尝试过把FetionSDK.dll变成一个COM+程序,但是不巧的是这个dll没有strong name,无法转换。
引用完SDK后,在主窗体的uses下添加NullStudio.Fetion_SDK。
二、准备工作
准备工作很简单,在public区分符下,建立一个名为sdk的FetionSDK对象。然后为它创建一个实例。
三、用户登录
使用以下代码来填入用户名和密码:
sdk.AccountManager.FillUserIdAndPassword(UserID,Password,true);
三个参数分别是用户手机号,密码,是否自动登录,当选定了自动登录为True时,可以使用
sdk.AccountManager.LoginOrLogout();
来自动的判断是登录或是注销,否则的话,就要使用
sdk.AccountManager.Login(); 来登录。
四、状态改变
用户登录或注销,或是改变自己当前的状态时,会触发状态改变的事件。如下:
procedure sdk_SDK_UserSatusChange(sender: Object; e:UserSatusChangedEventArgs);
其中参数e中来自命名空间Imps.Client.Core。
然后我们绑定这个事件:
sdk.SDK_UserSatusChange += new FetionSDK.SDK_UserSatusChangedEventHandler(sdk_SDK_UserSatusChange);
这样SDK就能接收到状态改变的事件了。另外,还能针对各个状态,执行不同的指令,如下:
case e.NewStatus of
Imps.Client.UserAccountStatus.Disconnected: ;
Imps.Client.UserAccountStatus.Initialized: ;
Imps.Client.UserAccountStatus.Loginning: ;
Imps.Client.UserAccountStatus.Logon:
Imps.Client.UserAccountStatus.Logoff: ;
Imps.Client.UserAccountStatus.Logouting: ;
Imps.Client.UserAccountStatus.None: ;
Imps.Client.UserAccountStatus.OfflineLogon: ;
Imps.Client.UserAccountStatus.StandBy: ;
Imps.Client.UserAccountStatus.WaitReconnect: ;
else
end;
五、获取好友列表
var
lst : List<Contact>;
i: Integer;
begin
lstFriendLst.Items.Clear();
lst := sdk.ContactControl.getAllContactList();
for i := 0 to lst.Count - 1 do
begin
lstFriendLst.Items.Add(
string.Format("{0} [Fetion: {1} Mobile: {2}]",
lst[i].DisplayName, lst[i].Uri.Id,
IfThen(lst[i].PersonalInfo.MobileNo = string.Empty, "Not Published", lst[i].PersonalInfo.MobileNo)));
end;
六、发送消息
调用SDK的发送消息指令,传入的参数分别是对方手机号码和短信的内容。
sdk.ContactControl.SendIM.SendIM(edtPhoneNo.Text, edtSendMsg.Text);
七、接收消息
接收到消息时,会解发SDK的收到消息事件,如下:
procedure sdk_SDK_ReceiveMessage(sender: Object; e:SDK_ReceiveMessageEventArgs);
实现此方法后,绑定这个事件。
sdk.SDK_ReceiveMessage += new FetionSDK.SDK_ReceiveMessageEventHandler(sdk_SDK_ReceiveMessage);
八、发送手机短信
与发送消息一样,只不过使用的是另一个方法。
sdk.ContactControl.SendSMS.SendSMS(sdk.ContactControl.getMyself.Uri.Id,edtSendMsg.Text);
在这里需要注意SendIM与SendSMS的区别。
getMyself是SDK中的一个方法,用来获取当前用户的信息。
九、出错事件
当SDK因为某种原因出错后,会触发出错事件,如下:
procedure sdk_SDK_Error(sender: Object; e: SDK_ErrorEventArgs);
实现后绑定:
sdk.SDK_Error += new FetionSDK.SDK_ErrorEventHandler(sdk_SDK_Error);
十、编译,执行程序
现在可以编译并执行程序了。
十一、程序源码
namespace FetionDemo;
interface
uses
System.Windows.Forms,
System.Drawing,
Imps.Client.Core,
NullStudio.Fetion_SDK,
NullStudio.Fetion_SDK.Event,
System.Collections.Generic;
type
/// <summary>
/// Summary description for MainForm.
/// </summary>
MainForm = class(System.Windows.Forms.Form)
{$REGION Windows Form Designer generated fields}
private
btnSendSelf: System.Windows.Forms.Button;
edtPhoneNo: System.Windows.Forms.TextBox;
btnSend: System.Windows.Forms.Button;
edtSendMsg: System.Windows.Forms.TextBox;
sbMain: System.Windows.Forms.StatusStrip;
lblPassword: System.Windows.Forms.Label;
lblAccount: System.Windows.Forms.Label;
edtMsg: System.Windows.Forms.TextBox;
lblPhoneNo: System.Windows.Forms.Label;
gbMsg: System.Windows.Forms.GroupBox;
lstFriendLst: System.Windows.Forms.ListBox;
gbFriendLst: System.Windows.Forms.GroupBox;
edtPassword: System.Windows.Forms.TextBox;
edtUserID: System.Windows.Forms.TextBox;
lblError: System.Windows.Forms.ToolStripStatusLabel;
btnLogoff: System.Windows.Forms.Button;
btnLogin: System.Windows.Forms.Button;
lblStatus: System.Windows.Forms.ToolStripStatusLabel;
gbLogin: System.Windows.Forms.GroupBox;
components: System.ComponentModel.Container := nil;
method InitializeComponent;
{$ENDREGION}
private
method btnSendSelf_Click(sender: System.Object; e: System.EventArgs);
method btnSend_Click(sender: System.Object; e: System.EventArgs);
method btnLogoff_Click(sender: System.Object; e: System.EventArgs);
method btnLogin_Click(sender: System.Object; e: System.EventArgs);
method MainForm_Load(sender: System.Object; e: System.EventArgs);
protected
method Dispose(aDisposing: boolean); override;
function IfThen(ABool: Boolean; AStr1, AStr2: String): String;
public
sdk : FetionSDK;
bLogon: Boolean;
procedure sdk_SDK_UserSatusChange(sender: Object; e:UserSatusChangedEventArgs);
procedure sdk_SDK_Error(sender: Object; e: SDK_ErrorEventArgs);
procedure sdk_SDK_ReceiveMessage(sender: Object; e:SDK_ReceiveMessageEventArgs);
constructor;
end;
implementation
{$REGION Construction and Disposition}
constructor MainForm;
begin
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
end;
method MainForm.Dispose(aDisposing: boolean);
begin
if aDisposing then begin
if assigned(components) then
components.Dispose();
//
// TODO: Add custom disposition code here
//
end;
inherited Dispose(aDisposing);
end;
{$ENDREGION}
{$REGION Windows Form Designer generated code}
method MainForm.InitializeComponent;
begin
var resources: System.ComponentModel.ComponentResourceManager := new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
self.sbMain := new System.Windows.Forms.StatusStrip();
self.lblStatus := new System.Windows.Forms.ToolStripStatusLabel();
self.lblError := new System.Windows.Forms.ToolStripStatusLabel();
self.gbMsg := new System.Windows.Forms.GroupBox();
self.lblPhoneNo := new System.Windows.Forms.Label();
self.edtSendMsg := new System.Windows.Forms.TextBox();
self.btnSend := new System.Windows.Forms.Button();
self.edtPhoneNo := new System.Windows.Forms.TextBox();
self.edtMsg := new System.Windows.Forms.TextBox();
self.btnLogin := new System.Windows.Forms.Button();
self.edtUserID := new System.Windows.Forms.TextBox();
self.edtPassword := new System.Windows.Forms.TextBox();
self.lblAccount := new System.Windows.Forms.Label();
self.lblPassword := new System.Windows.Forms.Label();
self.gbLogin := new System.Windows.Forms.GroupBox();
self.btnLogoff := new System.Windows.Forms.Button();
self.gbFriendLst := new System.Windows.Forms.GroupBox();
self.lstFriendLst := new System.Windows.Forms.ListBox();
self.btnSendSelf := new System.Windows.Forms.Button();
self.sbMain.SuspendLayout();
self.gbMsg.SuspendLayout();
self.gbLogin.SuspendLayout();
self.gbFriendLst.SuspendLayout();
self.SuspendLayout();
//
// sbMain
//
self.sbMain.Items.AddRange(array of System.Windows.Forms.ToolStripItem([self.lblStatus,
self.lblError]));
self.sbMain.Location := new System.Drawing.Point(0, 466);
self.sbMain.Name := 'sbMain';
self.sbMain.Size := new System.Drawing.Size(358, 22);
self.sbMain.TabIndex := 9;
self.sbMain.Text := 'statusStrip1';
//
// lblStatus
//
self.lblStatus.Name := 'lblStatus';
self.lblStatus.Size := new System.Drawing.Size(41, 17);
self.lblStatus.Text := 'Logoff';
//
// lblError
//
self.lblError.Name := 'lblError';
self.lblError.Size := new System.Drawing.Size(53, 17);
self.lblError.Text := 'No Error';
//
// gbMsg
//
self.gbMsg.Controls.Add(self.lblPhoneNo);
self.gbMsg.Controls.Add(self.edtSendMsg);
self.gbMsg.Controls.Add(self.btnSend);
self.gbMsg.Controls.Add(self.edtPhoneNo);
self.gbMsg.Controls.Add(self.edtMsg);
self.gbMsg.Location := new System.Drawing.Point(12, 198);
self.gbMsg.Name := 'gbMsg';
self.gbMsg.Size := new System.Drawing.Size(337, 231);
self.gbMsg.TabIndex := 8;
self.gbMsg.TabStop := false;
self.gbMsg.Text := 'Message';
//
// lblPhoneNo
//
self.lblPhoneNo.AutoSize := true;
self.lblPhoneNo.Location := new System.Drawing.Point(11, 174);
self.lblPhoneNo.Name := 'lblPhoneNo';
self.lblPhoneNo.Size := new System.Drawing.Size(53, 12);
self.lblPhoneNo.TabIndex := 6;
self.lblPhoneNo.Text := 'Phone No';
//
// edtSendMsg
//
self.edtSendMsg.Location := new System.Drawing.Point(7, 198);
self.edtSendMsg.Name := 'edtSendMsg';
self.edtSendMsg.Size := new System.Drawing.Size(249, 21);
self.edtSendMsg.TabIndex := 5;
//
// btnSend
//
self.btnSend.Location := new System.Drawing.Point(262, 196);
self.btnSend.Name := 'btnSend';
self.btnSend.Size := new System.Drawing.Size(69, 23);
self.btnSend.TabIndex := 4;
self.btnSend.Text := 'Send';
self.btnSend.UseVisualStyleBackColor := true;
self.btnSend.Click += new System.EventHandler(@self.btnSend_Click);
//
// edtPhoneNo
//
self.edtPhoneNo.Location := new System.Drawing.Point(68, 171);
self.edtPhoneNo.Name := 'edtPhoneNo';
self.edtPhoneNo.Size := new System.Drawing.Size(145, 21);
self.edtPhoneNo.TabIndex := 3;
//
// edtMsg
//
self.edtMsg.Location := new System.Drawing.Point(5, 20);
self.edtMsg.Multiline := true;
self.edtMsg.Name := 'edtMsg';
self.edtMsg.ReadOnly := true;
self.edtMsg.ScrollBars := System.Windows.Forms.ScrollBars.Vertical;
self.edtMsg.Size := new System.Drawing.Size(326, 145);
self.edtMsg.TabIndex := 1;
//
// btnLogin
//
self.btnLogin.Location := new System.Drawing.Point(274, 12);
self.btnLogin.Name := 'btnLogin';
self.btnLogin.Size := new System.Drawing.Size(75, 23);
self.btnLogin.TabIndex := 6;
self.btnLogin.Text := 'Login';
self.btnLogin.UseVisualStyleBackColor := true;
self.btnLogin.Click += new System.EventHandler(@self.btnLogin_Click);
//
// edtUserID
//
self.edtUserID.Location := new System.Drawing.Point(69, 17);
self.edtUserID.Name := 'edtUserID';
self.edtUserID.Size := new System.Drawing.Size(180, 21);
self.edtUserID.TabIndex := 3;
//
// edtPassword
//
self.edtPassword.Location := new System.Drawing.Point(69, 44);
self.edtPassword.Name := 'edtPassword';
self.edtPassword.PasswordChar := '*';
self.edtPassword.Size := new System.Drawing.Size(180, 21);
self.edtPassword.TabIndex := 4;
//
// lblAccount
//
self.lblAccount.AutoSize := true;
self.lblAccount.Location := new System.Drawing.Point(6, 20);
self.lblAccount.Name := 'lblAccount';
self.lblAccount.Size := new System.Drawing.Size(47, 12);
self.lblAccount.TabIndex := 1;
self.lblAccount.Text := 'Accoumt';
//
// lblPassword
//
self.lblPassword.AutoSize := true;
self.lblPassword.Location := new System.Drawing.Point(6, 47);
self.lblPassword.Name := 'lblPassword';
self.lblPassword.Size := new System.Drawing.Size(53, 12);
self.lblPassword.TabIndex := 2;
self.lblPassword.Text := 'Password';
//
// gbLogin
//
self.gbLogin.Controls.Add(self.edtPassword);
self.gbLogin.Controls.Add(self.edtUserID);
self.gbLogin.Controls.Add(self.lblAccount);
self.gbLogin.Controls.Add(self.lblPassword);
self.gbLogin.Location := new System.Drawing.Point(11, 6);
self.gbLogin.Name := 'gbLogin';
self.gbLogin.Size := new System.Drawing.Size(255, 80);
self.gbLogin.TabIndex := 7;
self.gbLogin.TabStop := false;
self.gbLogin.Text := 'Login';
//
// btnLogoff
//
self.btnLogoff.Location := new System.Drawing.Point(274, 41);
self.btnLogoff.Name := 'btnLogoff';
self.btnLogoff.Size := new System.Drawing.Size(75, 23);
self.btnLogoff.TabIndex := 10;
self.btnLogoff.Text := 'Logoff';
self.btnLogoff.UseVisualStyleBackColor := true;
self.btnLogoff.Click += new System.EventHandler(@self.btnLogoff_Click);
//
// gbFriendLst
//
self.gbFriendLst.Controls.Add(self.lstFriendLst);
self.gbFriendLst.Location := new System.Drawing.Point(11, 92);
self.gbFriendLst.Name := 'gbFriendLst';
self.gbFriendLst.Size := new System.Drawing.Size(338, 100);
self.gbFriendLst.TabIndex := 11;
self.gbFriendLst.TabStop := false;
self.gbFriendLst.Text := 'Friend List';
//
// lstFriendLst
//
self.lstFriendLst.FormattingEnabled := true;
self.lstFriendLst.ItemHeight := 12;
self.lstFriendLst.Location := new System.Drawing.Point(8, 18);
self.lstFriendLst.Name := 'lstFriendLst';
self.lstFriendLst.Size := new System.Drawing.Size(324, 76);
self.lstFriendLst.TabIndex := 0;
//
// btnSendSelf
//
self.btnSendSelf.Location := new System.Drawing.Point(243, 435);
self.btnSendSelf.Name := 'btnSendSelf';
self.btnSendSelf.Size := new System.Drawing.Size(106, 23);
self.btnSendSelf.TabIndex := 12;
self.btnSendSelf.Text := 'Send To Self';
self.btnSendSelf.UseVisualStyleBackColor := true;
self.btnSendSelf.Click += new System.EventHandler(@self.btnSendSelf_Click);
//
// MainForm
//
self.ClientSize := new System.Drawing.Size(358, 488);
self.Controls.Add(self.btnSendSelf);
self.Controls.Add(self.gbFriendLst);
self.Controls.Add(self.btnLogoff);
self.Controls.Add(self.sbMain);
self.Controls.Add(self.gbMsg);
self.Controls.Add(self.btnLogin);
self.Controls.Add(self.gbLogin);
self.Icon := (resources.GetObject('$this.Icon') as System.Drawing.Icon);
self.Name := 'MainForm';
self.Text := 'Fetion';
self.Load += new System.EventHandler(@self.MainForm_Load);
self.sbMain.ResumeLayout(false);
self.sbMain.PerformLayout();
self.gbMsg.ResumeLayout(false);
self.gbMsg.PerformLayout();
self.gbLogin.ResumeLayout(false);
self.gbLogin.PerformLayout();
self.gbFriendLst.ResumeLayout(false);
self.ResumeLayout(false);
self.PerformLayout();
end;
{$ENDREGION}
method MainForm.MainForm_Load(sender: System.Object; e: System.EventArgs);
begin
sdk := new FetionSDK;
bLogon := false;
sdk.SDK_UserSatusChange += new FetionSDK.SDK_UserSatusChangedEventHandler(sdk_SDK_UserSatusChange);
sdk.SDK_ReceiveMessage += new FetionSDK.SDK_ReceiveMessageEventHandler(sdk_SDK_ReceiveMessage);
sdk.SDK_Error += new FetionSDK.SDK_ErrorEventHandler(sdk_SDK_Error);
end;
procedure MainForm.sdk_SDK_UserSatusChange(sender: Object; e:UserSatusChangedEventArgs);
var
currStat: string;
i: Integer;
lst : List<Contact>;
begin
currStat := e.NewStatus.ToString();
lblStatus.Text := currStat;
case e.NewStatus of
Imps.Client.UserAccountStatus.Logon:
begin
bLogon := true;
lstFriendLst.Items.Clear();
lst := sdk.ContactControl.getAllContactList();
for i := 0 to lst.Count - 1 do
begin
lstFriendLst.Items.Add(
string.Format("{0} [Fetion: {1} Mobile: {2}]",
lst[i].DisplayName, lst[i].Uri.Id,
IfThen(lst[i].PersonalInfo.MobileNo = string.Empty, "Not Published", lst[i].PersonalInfo.MobileNo)));
end;
end;
else
begin
bLogon := false;
lstFriendLst.Items.Clear();
end;
{
Imps.Client.UserAccountStatus.Disconnected: ;
Imps.Client.UserAccountStatus.Initialized: ;
Imps.Client.UserAccountStatus.Loginning: ;
Imps.Client.UserAccountStatus.Logoff: ;
Imps.Client.UserAccountStatus.Logouting: ;
Imps.Client.UserAccountStatus.None: ;
Imps.Client.UserAccountStatus.OfflineLogon: ;
Imps.Client.UserAccountStatus.StandBy: ;
Imps.Client.UserAccountStatus.WaitReconnect: ;
}
end;
end;
method MainForm.btnLogin_Click(sender: System.Object; e: System.EventArgs);
begin
if not bLogon then
begin
sdk.AccountManager.FillUserIdAndPassword(edtUserID.Text,edtPassword.Text,false);
sdk.AccountManager.LoginOrLogout();
end;
end;
procedure MainForm.sdk_SDK_Error(sender: Object; e: SDK_ErrorEventArgs);
begin
lblError.Text := e.Message.Message;
end;
method MainForm.btnLogoff_Click(sender: System.Object; e: System.EventArgs);
begin
if bLogon then
sdk.AccountManager.LoginOrLogout();
end;
function MainForm.IfThen(ABool: Boolean; AStr1, AStr2: String): String;
begin
if ABool then
result := AStr1
else
result := AStr2;
end;
method MainForm.btnSend_Click(sender: System.Object; e: System.EventArgs);
begin
sdk.ContactControl.SendIM.SendIM(edtPhoneNo.Text, edtSendMsg.Text);
edtMsg.Text := edtMsg.Text + 'Self: ' + edtSendMsg.Text + '\r\n';
end;
procedure MainForm.sdk_SDK_ReceiveMessage(sender: Object; e:SDK_ReceiveMessageEventArgs);
begin
edtMsg.Text := edtMsg.Text + e.Contact.DisplayName + ': '+e.Message;
end;
method MainForm.btnSendSelf_Click(sender: System.Object; e: System.EventArgs);
begin
sdk.ContactControl.SendSMS.SendSMS(sdk.ContactControl.getMyself.Uri.Id,edtSendMsg.Text);
end;
end.
使用飞信SDK开发短信收发程序相关推荐
- 开发短信发送程序的几则技巧
主要针对smpp协议(扩展协议:cmpp等). 1. check resp status and process it, such as incorrect bind status; 2. contr ...
- gsm基于linux程序,基于嵌入式Linux下GSM模块的短信收发系统设计
摘要:移动通信中的短消息业务以其方便.可靠和价廉得到了广泛应用,本文在嵌入式Linux系统中,通过西门子MC35模块,实现了短信收发系统,该系统采用PDU短信模式,能支持中文短信.程序设计采用异步事件 ...
- 基于AT指令开发短信程序
基于AT指令开发短信程序 本人的专职工作是做手机底层软件中SMS和CBS的功能模块软件,对SMS的PDU格式可以说是比较了解,在网上查找了一下感觉目前国内公开的软件大多功能比较单一.主要特点如下: 1 ...
- 关于联通短信发送程序(SGIP协议)没有收到返回状态报告的报告(使用WireShark分析)
关于联通短信发送程序(SGIP协议)没有收到返回状态报告的报告(使用WireShark分析) 这是数年前给一个公司做的:使用WireShark抓包分析程序问题:是使用WireShark分析程序涉及到网 ...
- 调用WebServices接口实现短信收发
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 单片机控制GSM模块实现短信收发的软件设计
摘要:借助系统模型,阐明GSM模块收发短信的基本概念以及串口控制SMS的基本原理.详细介绍单片机控制GSM模块工作的软件实现过程,对怎样用单片机控制GSM模块收发短信进行探讨,也对程序设计的主体思想作 ...
- 【物联网智能网关-03】GPRS模块中文短信收发
在去年年初,就已经推出V1.0.0的GPRS库,不过在这个版本上只是实现了西文短信收发和字符串方式的GPRS数据通信,功能还相对不完善(参见我以前的博文<GPRS通信实现>).最近升级的版 ...
- Symbian Belle短信备份程序
最近我用Qt写了一个symbian belle的短信备份程序,它模拟了之前塞班S60V3的短信备份程序smsdiary,备份出来的效果也类似.有需要的同学请下载使用.我一直备份自己的短信,之前用sms ...
- SMSLib实现Java短信收发的功能
blog迁移至: http://www.micmiu.com 用java实现短信收发的功能,目前一般项目中短信群发功能的实现方法大致有下面三种: 1. 向运行商申请短信网关,不需要额外的设备,利用运行 ...
最新文章
- 常见数据结构与算法:拆分数字(暴力递归,动态规划)
- 上拉加载你这个坑货~
- css选择器的优先级
- 美好生活从撸好代码开始
- php截取3位数,使用php实现截取指定长度
- plsql 存储过程 批量提交_浅谈PetShop之使用存储过程与PLSQL批量处理(附案例)
- 3 缓存文件写入失败_分布式缓存数据库一致性问题
- SpringSecurity简单教程(源码开源免费提供)
- html影院选座模板,jQuery实现影院选座订座效果
- Dell 禁用触摸板
- wps如何只让他显示3级标题_怎么设置一二三级标题
- sql中简简单单明明白白得使用 with temp as (select x from table1)
- python中 and 和 or 操作的返回值。
- sql语句使数据指数增长
- Ambire钱包白皮书
- 计算机网络基础知识总结及思维导图(一)概述
- php 车牌号限号,不限行也不限号!还能送车牌?这种车你考虑吗?
- node.js搭建文件服务器,Node.js创建HTTP文件服务器的使用示例
- IDEA 报错 project is already registered
- python使用列表推导式生成5个数字8的列表_【菜鸟学Python】列表推导式
热门文章
- 第二周-第07章节-Python3.5-列表的使用(1)
- 免费开源CDN jsDelivr使用
- 体育学校有没有计算机专业,职高有体育专业吗
- 软件需求工程 高校教学平台 用户手册
- java获取文件大小_Java实现获取文件大小的几种方法
- android:completionthreshold=1,android:completionThreshold=1是哪个组件的属性( )
- mybatis不区分大小写问题
- 巴别塔合约终端开发日记1-----技术选择
- Pytorch预训练模型加载
- 消息中间件ActiveMQ 4: 传输协议