Click here to Skip to main content
15,885,914 members
Articles / Programming Languages / C#

Windows Mobile 5 Notifications

Rate me:
Please Sign up or sign in to vote.
4.47/5 (8 votes)
25 Feb 2008CPOL1 min read 73.4K   1.3K   65   13
How to use Windows Mobile 5 Network Notifications to initiate data replication.
Win_Mobile5_Notifications/simple_snapi1.jpgWin_Mobile5_Notifications/simple_snapi2.jpg

Introduction

In this article, I describe how to use the Windows Mobile 5 State and Notification API (SNAPI) to get notified of changes in network states. Integrating network notification is the basis to implementing background data replication. Furthermore, when an application has knowledge of available networks, it can then choose the best connection to move data between the device and a back-end server.

Background

The State and Notifications APIs provide a powerful built-in notification broker structure for the Windows Mobile 5.0 environment. Some of the requirements of SNAPI are:

  • .NET Compact Framework Version 2.0
  • Exposed via Microsoft.WindowsMobile.Status namespace
  • Specific to the Windows Mobile 5.0 platform
  • Included in the OS installation (not in the deployment CABs for the .NET CF 2.0) and Emulator

Using the Code

To implement application notifications, a reference to the following namespaces must be added:

  • Microsoft.WindowsMobile
  • Microsoft.WindowsMobile.Status

The following code outlines how to enable the notification callbacks within the application:

C#
public void SetUpNotifications()
{
    // This tells which states to monitor
    SystemState s;
    // Monitor for ActiveSync Connection
    s = new SystemState(SystemProperty.CradlePresent);
    s.Changed += new ChangeEventHandler(ChangeOccurred);
    stateList.Add(s);
    // Monitor for GPRS Connection
    s = new SystemState(SystemProperty.PhoneGprsCoverage);
    s.Changed += new ChangeEventHandler(ChangeOccurred);
    stateList.Add(s);
    //Monitor for Network Connection (e.g. WiFi)
    s = new SystemState(SystemProperty.ConnectionsNetworkCount);
    s.Changed += new ChangeEventHandler(ChangeOccurred);
    stateList.Add(s);

    UpdateConnectionState();
}

public void ChangeOccurred(object sender, ChangeEventArgs args)
{
    // If a change occurs
    SystemState state = (SystemState)sender;
    UpdateConnectionState();
}

public void UpdateConnectionState()
{
    // Set the check boxes based on the current state of the networks
    activesync.Checked = Convert.ToBoolean
        (SystemState.GetValue(SystemProperty.CradlePresent));
    gprs.Checked = Convert.ToBoolean(SystemState.GetValue
        (SystemProperty.PhoneGprsCoverage));
    wifi.Checked = Convert.ToBoolean(SystemState.GetValue
        (SystemProperty.ConnectionsNetworkCount));
}

Timers are a useful way of using the notification APIs to help determine when to initiate replication. On a specific interval, a function can be called to initiate data movement based on network availability. The following code shows how the current state of the network can help determine which data to retrieve.

C#
private void Sync()
{
    // Initiate A Connection Using the preferred (available) network connection

    if (activesync.Checked)
    {
        // Retrieve all data
    }
    else if (wifi.Checked)
    {
        // Retrieve medium priority data
    }
    else if (gprs.Checked)
    {
        // Retrieve high priority data
    }
    else
    {
        txtStatus.Text = "No Connectivity.";
        return;
    }
}

Conclusion

SNAPI is a excellent addition to a mobile application and can help to optimize data exchange. By adding a mobile database, an application can then be built to move data in the background to the point that users do not even realize synchronization is being executed.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Liam Cavanagh currently works as a Sr. Program Manager for Microsoft focusing new cloud services in the SQL Azure organization. Within this group, Liam has created a number of new services including SQL Azure Data Sync, Microsoft Codename "Data Transfer", and Eye on Earth leveraging the Windows Azure infrastucture.

Additionally, he works with enterprise corporations implementing enterprise cloud and mobile solutions and conducts training seminars worldwide.

Liam holds a Bachelor of Mathematics degree in Business and Information Systems from the University of Waterloo in Waterloo, Ontario, Canada.

Specialties: Windows Azure, SQL Azure, SQL Server, Cloud, Mobile, Replication and Database Synchronization Technologies

Liam Cavanagh is the founder of Cotega, a data notificaton and scheduling service for cloud databases. You can contact Liam at his cloud data blog.

Comments and Discussions

 
GeneralMy vote of 1 Pin
hfrmobile6-Sep-10 1:48
hfrmobile6-Sep-10 1:48 
GeneralSNAPI in native code Pin
Member 38770655-Mar-08 2:19
Member 38770655-Mar-08 2:19 
GeneralWindows Mobile 5 vs Windows CE 5 Pin
brian2510-Aug-07 16:16
brian2510-Aug-07 16:16 
AnswerRe: Windows Mobile 5 vs Windows CE 5 Pin
Liam Cavanagh11-Aug-07 12:21
Liam Cavanagh11-Aug-07 12:21 
GeneralRe: Windows Mobile 5 vs Windows CE 5 Pin
brian2511-Aug-07 12:33
brian2511-Aug-07 12:33 
Generalsyncronisation Pin
pmartike3-Jul-07 21:01
pmartike3-Jul-07 21:01 
AnswerRe: syncronisation Pin
Liam Cavanagh4-Jul-07 2:59
Liam Cavanagh4-Jul-07 2:59 
Generalconnection Pin
Mohammed.Reda8-Jun-07 6:42
Mohammed.Reda8-Jun-07 6:42 
AnswerRe: connection Pin
Liam Cavanagh8-Jun-07 7:19
Liam Cavanagh8-Jun-07 7:19 
GeneralRe: connection Pin
Mohsen_Emgineer200411-Jun-07 13:47
Mohsen_Emgineer200411-Jun-07 13:47 
GeneralRe: connection Pin
Liam Cavanagh12-Jun-07 3:58
Liam Cavanagh12-Jun-07 3:58 
GeneralRe: connection Pin
Mohsen_Emgineer200412-Jun-07 16:31
Mohsen_Emgineer200412-Jun-07 16:31 
GeneralRe: connection Pin
Liam Cavanagh13-Jun-07 2:14
Liam Cavanagh13-Jun-07 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.