如果想要設計一支程式來電自動接聽要怎來做怎呢,我們可以使用Microsoft.WindowsMobile.Status.SystemState類別所提供的PhoneIncomingCall事件來驅動模擬按下畫面中的接聽鍵達成動作

Step1:開啟vs2008新增一個vb 智慧型裝置專案,接著將滑鼠移到方案總管點選參考,在按下右鍵點選功能表上的加入參考

Step2:點選Microsoft.WindowsMobile'、Microsoft.WindowsMobile.Status二個元件在按下確定

Step3:接下來在表單裡面撰寫程式


Imports ...Microsoft.WindowsMobile.Status
Public Class Form1
Friend WithEvents Phone
As New SystemState(SystemProperty.PhoneIncomingCall)

Private Declare Sub keybd_event Lib "coredll.dll" () Declare Sub keybd_event Lib "coredll.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Const VK_F3 As Integer = &H72
Const VK_F16 As Integer = &H7F
Const KEYEVENTF_KEYUP As Integer = &H2
'利用手機事件的來電時會觸發此事件

Private Sub Phone_Changed() Sub Phone_Changed(ByVal sender As Object, ByVal args As Microsoft.WindowsMobile.Status.ChangeEventArgs) Handles Phone.Changed
'判斷事件回傳值為1掛斷為0,若為1的時候就去執行接聽
If args.NewValue = "1" Then
'要讓程式等一下,系統會發出是否接聽的對話方塊才能觸發接聽動作
System.Threading.Thread.Sleep(500)
Accpet()
ListBox1.Items.Add("Accpet")
'在次讓程式等一下,等到通話視窗出來才能去下喇叭開啟
System.Threading.Thread.Sleep(500)
Speaker()
ListBox1.Items.Add("Speaker")
End If
End Sub

Private Sub Accpet() Sub Accpet()
'模擬來電時按下接聽鍵
keybd_event(VK_F3, 0, 0, 0)
keybd_event(VK_F3, 0, KEYEVENTF_KEYUP, 0)
End Sub

Private Sub Speaker() Sub Speaker()
'模擬接電時按下喇叭開啟鍵
keybd_event(VK_F16, 0, 0, 0)
keybd_event(VK_F16, 0, KEYEVENTF_KEYUP, 0)
End Sub
End Class
Step4:按下vs2008功能表上偵錯\開始偵錯進行部署應用程式來測一下吧

Step5:用Cellular Emulator來測一下打個電話看看應用程式是否有接聽

Step6: 應用程式接聽完成後接著是開啟喇叭,可是我是用模擬器來做demo 所以會失效,你可以用實機來測一下就可以感受到

Step7: 最後看一下我們剛才所觸發事件的動作

Step8: 源碼下載