http://www.cnblogs.com/simalone/archive/2010/03/14/1601211.html
經過近段時間不斷在網的搜尋,終於今天下午有所斬獲了:
對於windows mobile的簡訊攔截,網上大概有兩種方法:
C++:微軟的SDK中提供一個Mapirule的例子。編譯好mapirule.dll後,對註冊表修改之類的就行了。因為我是用C#的,所以這個方法沒試。
C#:通過MessageInterceptor類實現。C++也可以使用這個方法。使用這個方法方便很多,可是就是,在程式失去焦點後就不能實現攔截了。
通過網上搜尋,得到解決程式失去焦點問題的方法:
參考MSDN:http://msdn.microsoft.com/en-us/bb932385.aspx
通過在註冊表中建立一個持久的語音總機,這樣在應用程式退出的時候,也能進行簡訊攔截了!
代碼大致如下:
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 }
關於過濾方面可能存在的問題,可以參考:
MessageInterceptor簡直就是個笑話,高手菜鳥都來看看:http://topic.csdn.net/u/20071204/17/c8946432-a979-4e8d-ba8d-f881a15bb7a0.html?seed=1883745506
關於攔截後的對不需要攔截的寫到SIM,可以參考
http://www.cnblogs.com/appleseeker/archive/2008/03/29/1129031.html
雖然到目前觀摩了很多關於windows mobile的簡訊防火牆的文章,但目前為止尚未找到對於C#的完善解決方案。真是cupTools呀!