代码改变世界

windows mobile开发自动重拨的软件

2011-01-25 12:38  pxeric  阅读(849)  评论(32)    收藏  举报

        春节到了,每年的火车票都是个头痛问题,记得去年我是用波导(手机中的战斗机)来打订票电话的。当时从6点多钟起床一直打,打到8、9点后那大拇指又痛又酸,因为一直在重复拨打、挂断这个步骤,累得我都怕打电话了。2010年6月份,我换了一台多普达的智能机,是windows mobile系统,前几天打订票电话发现比去年那个战斗机还更麻烦,还更累。所以就萌发了自己开发个自动重拨的软件来应对这次春节订票了。

        其实现的原理很简单,即:拨打订票电话---在设定的时间里未通----继续拨打-----若接通了----则振动设备并退出重拨软件。

        基本功能有:绑定本机联系人、输入号码模糊查询、重拨次数设置、重拨时间间隔设置、IP拨号、打开免提、接通振动、振动次数设置、接通退出

        运行环境:需.net compact framework2.0或以上,我是用多普达S910W测试的

        目前程序已基本完成,但有平台的兼容性问题(我的是多普达S910w,放同事的手机上会出现免提打不开等bug)。        

        下面是主要源代码:

          

    1using System; 

  2using System.Collections.Generic;
  3 using System.Windows.Forms;
  4 using Microsoft.WindowsMobile.PocketOutlook;
  5 using Microsoft.WindowsMobile.Telephony;
  6 using Microsoft.WindowsMobile.Status;
  7 using System.Text.RegularExpressions;
  8 
  9 namespace AutoRedial
 10 {
 11     public partial class Form1 : Form
 12     {
 13         System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); //定义定时器
 14         int AutoRedialCount = 0//重拨次数变量
 15         Phone phone = new Phone(); //实例化phone
 16         List<ContactModel> model = new List<ContactModel>(); //实例化联系人实体
 17         private SystemState callState, callingState; //定义通话的状态
 18         public Form1()
 19         {
 20             InitializeComponent();
 21             BindContact();
 22             SetDropDownListValue();
 23             callState = new SystemState(SystemProperty.PhoneCallTalking);
 24             callingState = new SystemState(SystemProperty.PhoneCallCalling);
 25             callState.Changed += new ChangeEventHandler(callState_Changed);
 26             callingState.Changed += new ChangeEventHandler(callingState_Changed);
 27         }
 28         /// <summary>
 29         /// 定义通话中状态的事件
 30         /// <remark>2011/1/24 20:10 By Eric </remark>
 31         /// </summary>
 32         /// <param name="sender">The sender.</param>
 33         /// <param name="args">The <see cref="Microsoft.WindowsMobile.Status.ChangeEventArgs"/> instance containing the event data.</param>
 34         void callState_Changed(object sender, ChangeEventArgs args)
 35         {
 36             if (args.NewValue != null)
 37             {
 38                 //String state = ((int)args.NewValue) == 1 ? "talking..." : "hang up";
 39                 if ((int)args.NewValue == 1//通话中
 40                 {
 41                     timer.Enabled = false;
 42                     if (chkzd.Checked) //振动设备
 43                     {
 44                         DropCall.Vibrate(int.Parse(ddlVibrateCount.Text), int.Parse(ddlhm.Text));
 45                     }
 46                     if (chkExit.Checked) //如果勾选接通后退出则退出
 47                     {
 48                         Application.Exit();
 49                     }
 50                 }
 51             }
 52         }
 53         /// <summary>
 54         /// 定义正在拨出电话的事件,同时打开免提(有bug)
 55         /// <remark>2011/1/24 20:33 By Eric </remark>
 56         /// </summary>
 57         /// <param name="sender">The sender.</param>
 58         /// <param name="args">The <see cref="Microsoft.WindowsMobile.Status.ChangeEventArgs"/> instance containing the event data.</param>
 59         void callingState_Changed(object sender, ChangeEventArgs args)
 60         {
 61             if (args.NewValue != null)
 62             {
 63                 if ((int)args.NewValue == 1)
 64                 {
 65                     System.Threading.Thread.Sleep(500); //休息500毫秒解决打开免提bug的问题
 66                     if (chkOpen.Checked) //打开免提
 67                     {
 68                         DropCall.OpenSpeaker(); //模拟按键的方式
 69                     }
 70                     if (AutoRedialCount > 0)
 71                         AutoRedialCount--; //重拨次数减1
 72                 }
 73             }
 74         }
 75 
 76         /// <summary>
 77         /// 设置dropdownlist的默认值
 78         /// <remark>2011/1/25 9:26 By Eric </remark>
 79         /// </summary>
 80         public void SetDropDownListValue()
 81         {
 82             ddlIPNumber.SelectedIndex = 0;
 83             ddlredialCount.SelectedIndex = 0;
 84         }
 85         /// <summary>
 86         /// 绑定联系人至DataGrid
 87         /// <remark>2011/1/20 12:56 By Eric </remark>
 88         /// </summary>
 89         public void BindContact()
 90         {
 91             using (OutlookSession s = new OutlookSession())
 92             {                
 93                 foreach (Contact c in s.Contacts.Items)
 94                 {
 95                     model.Add(new ContactModel { 联系人 = c.FileAs, 号码 = c.MobileTelephoneNumber });
 96                 }
 97                 dataGrid1.DataSource = model;
 98             }
 99         }
100 
101         /// <summary>
102         /// 选择联系人电话致文本框
103         /// <remark>2011/1/20 12:57 By Eric </remark>
104         /// </summary>
105         /// <param name="sender">The sender.</param>
106         /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
107         private void dataGrid1_Click(object sender, EventArgs e)
108         {
109             txtPhone.Text = dataGrid1[dataGrid1.CurrentCell.RowNumber, 1].ToString();
110         }
111 
112         /// <summary>
113         /// 开始拨号
114         /// <remark>2011/1/20 14:08 By Eric </remark>
115         /// </summary>
116         /// <param name="sender">The sender.</param>
117         /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
118         private void StartCall_Click(object sender, EventArgs e)
119         {
120             try
121             {
122                 if (txtPhone.Text.Trim() == "")
123                     throw new Exception("电话号码不能为空");
124                 if (!isNumber(txtPhone.Text))
125                     throw new Exception("电话号码不正确");
126                 Call(); //拨打电话
127                 int sleeptime = string.IsNullOrEmpty(ddlRedialSecond.Text) ? 5000 : (int.Parse(ddlRedialSecond.Text) * 1000);
128                 AutoRedialCount = ddlredialCount.Text == "无限" ? -1 : int.Parse(ddlredialCount.Text);//设定重拨次数
129                 timer.Interval = sleeptime;
130                 timer.Enabled = true;
131                 timer.Tick += new EventHandler(timer_Tick);
132             }
133             catch (Exception ex)
134             {
135                 timer.Enabled = false;
136                 MessageBox.Show("拨打失败,原因是:" + ex.Message, "操作失败");
137                 return;
138             }            
139         }
140 
141         /// <summary>
142         /// 结束拨号并退出程序
143         /// <remark>2011/1/22 11:31 By Eric </remark>
144         /// </summary>
145         /// <param name="sender">The sender.</param>
146         /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
147         private void EndCall_Click(object sender, EventArgs e)
148         {
149             timer.Enabled = false;
150             Application.Exit();
151         }
152         /// <summary>
153         /// timer事件
154         /// <remark>2011/1/22 12:41 By Eric </remark>
155         /// </summary>
156         /// <param name="sender">The sender.</param>
157         /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
158         void timer_Tick(object sender, EventArgs e)
159         {
160             if (!SystemState.PhoneCallTalking)
161             {
162                 if (AutoRedialCount == -1//无限次重拨
163                 {
164                     CallAgain();//再次拨打
165                 }
166                 else //设定了次数的拨打
167                 {
168                     if (AutoRedialCount > 0//判断拨打的次数是否已到
169                     {
170                         CallAgain();//再次拨打
171                     }
172                     else
173                     {
174                         timer.Enabled = false//取消定时器
175                         DropCall.Drop(); //挂断电话
176                     }
177                 }
178             }
179         }
180 
181         /// <summary>
182         /// 挂断当前电话,并重新拨打一次
183         /// <remark>2011/1/24 20:42 By Eric </remark>
184         /// </summary>
185         private void CallAgain()
186         {
187             DropCall.Drop(); //挂断电话
188             Call();
189         }
190 
191         /// <summary>
192         /// 建立通话
193         /// <remark>2011/1/20 14:38 By Eric </remark>
194         /// </summary>
195         public void Call()
196         {            
197             string IP = ddlIPNumber.Text == "IP拨号" ? "" : ddlIPNumber.Text; //获取IP拨号字符串
198             phone.Talk(IP + txtPhone.Text.Trim()); 
199         }
200         /// <summary>
201         /// 当在电话号码文本框上输入值时模糊查询联系人
202         /// <remark>2011/1/24 19:29 By Eric </remark>
203         /// </summary>
204         /// <param name="sender">The sender.</param>
205         /// <param name="e">The instance containing the event data.</param>
206         private void txtPhone_TextChanged(object sender, EventArgs e)
207         {
208             if (txtPhone.Text.Trim() != "")
209                 dataGrid1.DataSource = model.FindAll(new Predicate<ContactModel>(PersonPredicate));
210             else
211                 dataGrid1.DataSource = model;
212         }
213         /// <summary>
214         /// 表达式
215         /// <remark>2011/1/24 18:59 By Eric </remark>
216         /// </summary>
217         /// <param name="p">联系人实体</param>
218         /// <returns></returns>
219         public bool PersonPredicate(ContactModel p)
220         {
221             if (isNumber(txtPhone.Text)) //输入的为数字
222                 return p.号码.IndexOf(txtPhone.Text) > -1;
223             else //输入的为汉字
224                 return p.联系人.IndexOf(txtPhone.Text) > -1;
225         }
226 
227         /// <summary>
228         /// 判断是否为数字
229         /// </summary>
230         /// <param name="str">原字符串</param>
231         /// <returns></returns>
232         public bool isNumber(string str)
233         {
234             Regex reg = new Regex("^[0-9]+$");
235             Match ma = reg.Match(str);
236             return ma.Success;
237         }
238     }
239 }

下面是运行后的截图:

 

 

CAB和源码文件下载:/Files/pxeric/AutoRedial.rar 

由于本人是做web开发的,对winform和windows mobile开发不怎么懂,此次开发的这个软件是第一个windows mobile软件,所以恳请大家帮忙指出不足之处, 谢谢!