Detecting incoming phone calls

You saw earlier how you could detect incoming SMS messages using the MessageInterception class. How about incoming phone calls? You can do the same thing for phone calls but you'll need to use a different class—the SystemState class.

Note

The SystemState class is a useful class that allows you to query almost every type of state information on the mobile device. It allows you to query information such as battery life, missed phone calls, ActiveSync status, Bluetooth status, calendar events, appointment events, notification events, camera status, and phone signal strength, just to name a few. Not only can you query these statuses but you can also receive events in your code when these values change.

Let's take a look at the code you can use to do this. The method used to detect phone calls is roughly the same as the one used to detect incoming SMS messages. You need to set it to listen in the background for a specific status field and to register a call back function that can be automatically invoked when the status value changes. In the following code, we set it to listen to a status change in the incoming phone number:

using Microsoft.WindowsMobile.Status;
public void StartDetector()
{
_phoneCaller = new
SystemState(SystemProperty.PhoneIncomingCallerNumber,
true);
_phoneCaller.Changed += phoneCaller_Changed;
}

In the event handler, simply print the incoming phone number.

private void phoneCaller_Changed(object sender,
ChangeEventArgs args)
{
_IncomingphoneNumber =
Strings.Trim(SystemState.PhoneIncomingCallerNumber);
MessageBox.Show("The incoming call is from this number:
" + _IncomingphoneNumber,"");
}

The format of the phone number retrieved depends on the numbers you have stored in your Windows Mobile Contacts area. For instance, if you have a Windows Mobile Contact named My wife with the phone number +60163176148 and you receive a call from this number, the function preceding would yield:

My Wife <+60163176148>

If the number does not exist in your Windows Mobile Contacts area, it would simply return the number (without any brackets):

+60163176148

You can easily use regular expressions to parse and strip away all the unnecessary text if you want to extract only the phone number from this string.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.104.188