飞信SDK内容【转载】
生活随笔
收集整理的這篇文章主要介紹了
飞信SDK内容【转载】
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用飛信SDK開發(fā)短信收發(fā)程序 2008-10-13 13:57
| 利用飛信的協(xié)議可以在線收發(fā)消息,或是向手機(jī)發(fā)送消息。由此,可以自己來(lái)完成一個(gè)IM工具。 本文即是對(duì)飛信SDK的使用方法,及如何開發(fā)作一個(gè)說明。 一、引用FetionSDK 飛信是采用C#開發(fā)的,所有的程序集均是.NET,因此我們也需要使用Delphi.NET/Chrome來(lái)進(jìn)行相關(guān)的開發(fā)。在Chrome中,新建一個(gè)工程,并引入FetionSDK.dll,當(dāng)然您也可以使用Delphi2007 for .NET,開發(fā)出來(lái)結(jié)果一樣。 其實(shí)我曾嘗試過把FetionSDK.dll變成一個(gè)COM+程序,但是不巧的是這個(gè)dll沒有strong name,無(wú)法轉(zhuǎn)換。 引用完SDK后,在主窗體的uses下添加NullStudio.Fetion_SDK。 二、準(zhǔn)備工作 準(zhǔn)備工作很簡(jiǎn)單,在public區(qū)分符下,建立一個(gè)名為sdk的FetionSDK對(duì)象。然后為它創(chuàng)建一個(gè)實(shí)例。 三、用戶登錄 使用以下代碼來(lái)填入用戶名和密碼: sdk.AccountManager.FillUserIdAndPassword(UserID,Password,true); 三個(gè)參數(shù)分別是用戶手機(jī)號(hào),密碼,是否自動(dòng)登錄,當(dāng)選定了自動(dòng)登錄為True時(shí),可以使用 ?????? sdk.AccountManager.LoginOrLogout(); 來(lái)自動(dòng)的判斷是登錄或是注銷,否則的話,就要使用 sdk.AccountManager.Login(); 來(lái)登錄。 四、狀態(tài)改變 用戶登錄或注銷,或是改變自己當(dāng)前的狀態(tài)時(shí),會(huì)觸發(fā)狀態(tài)改變的事件。如下: procedure sdk_SDK_UserSatusChange(sender: Object; e:UserSatusChangedEventArgs); 其中參數(shù)e中來(lái)自命名空間Imps.Client.Core。 然后我們綁定這個(gè)事件: sdk.SDK_UserSatusChange += new FetionSDK.SDK_UserSatusChangedEventHandler(sdk_SDK_UserSatusChange); 這樣SDK就能接收到狀態(tài)改變的事件了。另外,還能針對(duì)各個(gè)狀態(tài),執(zhí)行不同的指令,如下: 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; 六、發(fā)送消息 調(diào)用SDK的發(fā)送消息指令,傳入的參數(shù)分別是對(duì)方手機(jī)號(hào)碼和短信的內(nèi)容。 sdk.ContactControl.SendIM.SendIM(edtPhoneNo.Text, edtSendMsg.Text); 七、接收消息 接收到消息時(shí),會(huì)解發(fā)SDK的收到消息事件,如下: procedure sdk_SDK_ReceiveMessage(sender: Object; e:SDK_ReceiveMessageEventArgs); 實(shí)現(xiàn)此方法后,綁定這個(gè)事件。 sdk.SDK_ReceiveMessage += new FetionSDK.SDK_ReceiveMessageEventHandler(sdk_SDK_ReceiveMessage); 八、發(fā)送手機(jī)短信 與發(fā)送消息一樣,只不過使用的是另一個(gè)方法。 sdk.ContactControl.SendSMS.SendSMS(sdk.ContactControl.getMyself.Uri.Id,edtSendMsg.Text); 在這里需要注意SendIM與SendSMS的區(qū)別。 getMyself是SDK中的一個(gè)方法,用來(lái)獲取當(dāng)前用戶的信息。 九、出錯(cuò)事件 當(dāng)SDK因?yàn)槟撤N原因出錯(cuò)后,會(huì)觸發(fā)出錯(cuò)事件,如下: procedure sdk_SDK_Error(sender: Object; e: SDK_ErrorEventArgs); 實(shí)現(xiàn)后綁定: sdk.SDK_Error += new FetionSDK.SDK_ErrorEventHandler(sdk_SDK_Error); 十、編譯,執(zhí)行程序 現(xiàn)在可以編譯并執(zhí)行程序了。 十一、程序源碼 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. |
轉(zhuǎn)載于:https://www.cnblogs.com/ddlzq/archive/2009/10/24/1589271.html
總結(jié)
以上是生活随笔為你收集整理的飞信SDK内容【转载】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 摩尔庄园粉海豚爱心池怎么获得
- 下一篇: 现在红眼还适合做鲁莽9件吗?