windows mobile 短信攔截

對於windows mobile的短信攔截,網上大概有兩種方法:

C++:微軟的SDK中提供一個Mapirule的例子。編譯好mapirule.dll後,對註冊表修改之類的就行了。因爲我是用C#的,所以這個方法沒試。

C#:通過MessageInterceptor類實現。C++也可以使用這個方法。使用這個方法方便很多,可是就是,在程序失去焦點後就不能實現攔截了。

通過網上搜索,得到解決程序失去焦點問題的方法:

參考MSDN:http://msdn.microsoft.com/en-us/bb932385.aspx

通過在註冊表中建立一個持久的信息通知,這樣在應用程序退出的時候,也能進行短信攔截了!

代碼大致如下:


 1 MessageInterceptor _smsInterceptor = null;
 2 const string _persistentIdentifier = "Contoso.Pharmaceuticals.MessageHandlerApp";
 3 
 4 private void Form1_Load(object sender, EventArgs e)
 5 {
 6     if ( ! MessageInterceptor.IsApplicationLauncherEnabled(_persistentIdentifier))
 7     {
 8         // Persistent notification does not yet exist - must explicitly create
 9         _smsInterceptor = new MessageInterceptor(InterceptionAction.NotifyAndDelete, false);
10         _smsInterceptor.MessageCondition = new MessageCondition(MessageProperty.Body, 
11             MessagePropertyComparisonType.StartsWith, "Contoso Data:"false);
12         // Make the interceptor persistent
13         _smsInterceptor.EnableApplicationLauncher(_persistentIdentifier);
14     }
15     else
16     {
17         // Persistent notification already defined - create this instance using the
18         // same characteristics
19         _smsInterceptor = new MessageInterceptor(_persistentIdentifier, false);
20     }
21 
22     // Once the interceptor is created, add event handler. Whether the interceptor is constructed
23     // explicitly or constructed from the persistent notification identifier, 
24     // the event handling behavior is the same.
25     _smsInterceptor.MessageReceived += SmsInterceptor_MessageReceived_OnThread;
26 
27 }
28 
29 // Notification runs on the message-interceptor thread, not the main
30 // application thread
31 void SmsInterceptor_MessageReceived_OnThread(object sender, MessageInterceptorEventArgs e)
32 {
33     SmsMessage newMessage = e.Message as SmsMessage;
34     if (newMessage != null)
35     {
36         // Cannot interact directly with user interface - in this case
37         // using an anonymous delegate with the BeginInvoke method to
38         // to transfer control to the main application thread to update
39         // the status bar
40         statusBar1.BeginInvoke(
41             (MethodInvoker)delegate { 
42                 statusBar1.Text = "From:" + newMessage.From.Address; 
43             });
44         Debug.WriteLine(string.Format("Sender:{0} - Body:{1}", newMessage.From.Address, newMessage.Body));
45     }
46 }
47 
48 
49 // Remove persistent notification 
50 private void menuDisablePersistentNotification_Click(object sender, EventArgs e)
51 {
52     // Confirm that _smsInterceptor is a valid reference, that the current 
53     // _smsInterceptor instance is associated with the correct persistent 
54     // notification identifier, and that a persistent notification exists
55     // that has the specified identifier
56     if (_smsInterceptor != null &&
57         _smsInterceptor.ApplicationLaunchId == _persistentIdentifier &&
58         MessageInterceptor.IsApplicationLauncherEnabled(_persistentIdentifier))
59     {
60         _smsInterceptor.DisableApplicationLauncher();
61     }
62 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章