[WM][VB][攔截簡訊]
當手機在家或是辦公室找不到怎麼辦,打電話鈴響就可以找到了沒錯,可是你的手機開震動怎麼辦,震到沒電還是找不到我們利用攔截訊息方式來播放beep直到你按下結束為止,那怎麼弄呢可以使用微軟提供Microsoft.WindowsMobile.PocketOutlook.MessageInterception類別來實作。
Step1:開啟vs2008新增一個vb 智慧型裝置專案,在畫面上拉一個Button元件用來停止beep,接著將滑鼠移到方案總管點選參考,在按下右鍵點選功能表上的加入參考
Step2:點選Microsoft.WindowsMobile.PocketOutlook、Microsoft.WindowsMobile二個元件在按下確定
Step3: 接下來在表單裡面撰寫程式
Imports Microsoft.WindowsMobile.PocketOutlook.MessageInterception
Public Class Form1
Private sms As MessageInterceptor
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'此簡訊只會被程式收到,手機的收件夾不會有
sms = New MessageInterceptor(InterceptionAction.NotifyAndDelete, True)
'設定攔截條件為訊息內容等於<SOS>字串
sms.MessageCondition = New MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.Equal, "<SOS>", True)
'MessageCondition的條件成立會觸發MessageReceived事件
AddHandler sms.MessageReceived, AddressOf sms_MessageReceived
End Sub
Private Sub sms_MessageReceived(ByVal sender As Object, ByVal e As Microsoft.WindowsMobile.PocketOutlook.MessageInterception.MessageInterceptorEventArgs)
'當欄截簡訊條件成立後就背景變成紅色,同時也啟動計時器
Me.BackColor = Color.Red
Me.Timer1.Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'設定時間到位就會觸發beep
Media.SystemSounds.Beep.Play()
End Sub
End Class
Public Class Form1
Private sms As MessageInterceptor
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'此簡訊只會被程式收到,手機的收件夾不會有
sms = New MessageInterceptor(InterceptionAction.NotifyAndDelete, True)
'設定攔截條件為訊息內容等於<SOS>字串
sms.MessageCondition = New MessageCondition(MessageProperty.Body, MessagePropertyComparisonType.Equal, "<SOS>", True)
'MessageCondition的條件成立會觸發MessageReceived事件
AddHandler sms.MessageReceived, AddressOf sms_MessageReceived
End Sub
Private Sub sms_MessageReceived(ByVal sender As Object, ByVal e As Microsoft.WindowsMobile.PocketOutlook.MessageInterception.MessageInterceptorEventArgs)
'當欄截簡訊條件成立後就背景變成紅色,同時也啟動計時器
Me.BackColor = Color.Red
Me.Timer1.Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'設定時間到位就會觸發beep
Media.SystemSounds.Beep.Play()
End Sub
End Class
Step4:按下vs2008功能表上偵錯\開始偵錯進行部署應用程式來測一下吧
Step5:用Cellular Emulator來測一下發一封簡訊內容跟我們要欄截字串不同,看看是否會被欄截。
Step6:接下來我們來發個簡訊是我們要欄截內容看手機是否會作動,成功後就按下Stop停止beep
Step7: 源碼下載