Windows Mobile 5.0 SDk Demo 分析 ——2 SmsIM

功能: 接受和发送短信,并在同一页面上模拟显示通信内容
       发送:从联系人名单中选择contact然后发信息给他
       接收:开机后随时监听是否收到信息,收到后进行显示

实现:
       本程序包含一个private类:Conversation 用来模拟显示一次短信接发的信息包括:联系人、电话号码、短息

内容。可以通过Contact、PhoneNumber、Transcript属性分别进行访问。
 
       发送:
       1.添加联系人
            点击 Add name,调用CreateNewConversation方法
            打开选择联系人窗口:
             ChooseContactDialog dlg = new ChooseContactDialog();
             dlg.RequiredProperties = new ContactProperty[] {ContactProperty.AllTextMessaging };
            选定联系人后添加到联系人列表中并创建一个Conversation实例。
       2.填写message
             首先调用SwitchToConversation方法,将刚才创建的conversation实例作为信息发送的目的地址。
             并将焦点跳到填写信息处。
       3.发送message
             点击send,调用SmsMessage的Send方法进行发送
            // Send the SMS message
            SmsMessage msg = new SmsMessage(NormalizePhoneNumber(conv.PhoneNumber), txtMsgToSend.Text);
            msg.Send();
               其中NormalizePhoneNumber方法用来将联系人的号码中非数字的信息剔除

            然后模拟显示信息并清空填写信息文本框
 
       接收:
       1.FormLoad时候初始化一个MessageInterceptor
            类成员变量:private MessageInterceptor interceptor;
            //初始化并注册一个信息收到的处理方法 OnSmsReceived
            interceptor = new MessageInterceptor();
            interceptor.InterceptionAction = InterceptionAction.NotifyAndDelete;
            interceptor.MessageReceived += new MessageInterceptorEventHandler(OnSmsReceived);
     
       2.当收到短信时,MessageInterceptor对象的事件处理方法:OnSmsReceived
             private void OnSmsReceived(object sender, MessageInterceptorEventArgs e)
              收到的短息信息全在(msg=)e.Message 中
          检查发送信息的地址(msg.)
              如果是联系人列表中的成员:
                 foreach (Conversation conv in conversations.Items)
                {
                    if (PhoneNumbersMatch(conv.PhoneNumber, msg.From.Address))
                        。。。
                }
              如果是新的联系人,查找到并添加到列表中
               foreach (Contact c in session.Contacts.Items)
                    {
                        if (c.MobileTelephoneNumber.Length != 0
                            && PhoneNumbersMatch(c.MobileTelephoneNumber, msg.From.Address))
                           。。。
                    }
              如果是尚未添加的联系人,将此联系人进行添加
              if (contact == null)
                    {
                        // There is no contact for this phone number
                        // so create one
                        contact = new Contact();
                        contact.MobileTelephoneNumber = msg.From.Address; //联系人地址
                        // TODO: prompt for name from user?
                        session.Contacts.Items.Add(contact);  //添加到联系人名单中
                    }
           将当前联系人作为当前对话参数,回到模拟页面进行信息显示
       3.Form1.close时候释放OnSmsReceived对象
              interceptor.Dispose();

ps:
1. RequiredProperties : Gets or sets the ContactProperty array values that specify which properties a

Contact must have in order to be shown in the Contact Chooser dialog box.
   获取或设置联系人属性值数组,规定这些属性值是必须出现在联系人信息中的。
   dlg.RequiredProperties = new ContactProperty[]{ ContactProperty.AllTextMessaging};

2.SmsMessage

      Represents a Short Message Service (SMS) message.  短息服务类。

     主要成员有:Body:文本内容, From:发送者,ItemID, LastModified最后修改日期,Read读取状态,Received接受与否,To 接受者集合,RequestDeliveryReport: 是否送达报告要求。

     主要方法有: Send、Tostring、Equals、GetType、GetHashCode、ReferenceEqual.

3.可以选择在联系人列表中默认添加了所有联系人信息
   #if PrePopulateConversationsComboBox
            using (OutlookSession session = new OutlookSession())
            {
                foreach (Contact c in session.Contacts.Items)
                {
                    if (c.MobileTelephoneNumber != "")
                    {
                        conversations.Items.Add(new Conversation(c));
                    }
                }
            }
   #endif
    
  
posted @ 2010-02-08 10:02  frankself008  阅读(274)  评论(0编辑  收藏  举报