none
寻求利用C# MessageInterceptor类进行短信拦截的解决机制 RRS feed

  • 问题

  • 我想用MessageInterceptor类在Windows Mobile手机上实现短信拦截,其实现目标是:应用程序能自动拦截指定号码发到本手机的短信,即使应用程序界面隐藏或在后台运行时也能进行拦截。现有代码如下:

      class SMSInterceptor
      {
        MessageInterceptor msgInterceptor;
    
        public void InterceptorSMS() 
        {
          if (MessageInterceptor.IsApplicationLauncherEnabled("123456"))
          {
            msgInterceptor = new MessageInterceptor("123456");
          }
          else 
          {
            msgInterceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete);
            msgInterceptor.MessageCondition = new MessageCondition(MessageProperty.Sender, MessagePropertyComparisonType.Contains, "123456");
    
            //string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;
            //msgInterceptor.EnableApplicationLauncher("123456", appPath);
          }
    
          msgInterceptor.MessageReceived += new MessageInterceptorEventHandler(msgInterceptor_MessageReceived);
        }
    
        private void msgInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs e)
        {
          string text = "Type Name:" + e.Message.GetType().Name + "\r\n";
          if (e.Message.GetType() == typeof(SmsMessage))
          {
            SmsMessage sms = (SmsMessage)e.Message;
            text += "Sender: " + sms.From.Name + "\r\n";
            text += "From: " + sms.From.Address + "\r\n";
            text += "Body: " + sms.Body + "\r\n";
            text += "Received Time: " + sms.Received.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss") + "\r\n";
          }
          MessageBox.Show(text);
        }
    
      }
    但用此段代码进行测试后发现有以下问题:
    (1)若将"string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;msgInterceptor.EnableApplicationLauncher("123456", appPath);"这两句代码注释掉,则应用程序在激活状态时,能拦截短信。但当应用程序的Form隐藏掉时,则短信无法拦截;
    (2)若将此两句启用,则在应用程序退出后,短信仍然能拦截。其原因是此两句启用后,在手机的注册表(HKEY-LOCAL-MACHINE\Software\Microsoft\Inbox\Rules)添加了拦截号码的对应项。每改变一个拦截号码,则注册表中就相应增加一项,只增不减。当然,针对这一情况,可以在应用程序代码中对注册表的项进行删减。但本人担心在各型WM手机上,注册表项Inbox\Rules对应的路径可能会不同。
    本人是一菜鸟,现在思路也有点混乱,望各位大侠不吝赐教,给出一个可行的解决方案。谢谢先!
    
    2010年9月5日 13:52

答案