Click here to Skip to main content
Click here to Skip to main content

Windows Mobile 5 Notifications

By , 25 Feb 2008
 
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:

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.

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)

About the Author

Liam Cavanagh
United States United States
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 1memberhfrmobile6 Sep '10 - 1:48 
GeneralSNAPI in native codememberMember 38770655 Mar '08 - 2:19 
GeneralWindows Mobile 5 vs Windows CE 5memberbrian2510 Aug '07 - 16:16 
AnswerRe: Windows Mobile 5 vs Windows CE 5memberLiam Cavanagh11 Aug '07 - 12:21 
GeneralRe: Windows Mobile 5 vs Windows CE 5memberbrian2511 Aug '07 - 12:33 
Generalsyncronisationmemberpmartike3 Jul '07 - 21:01 
AnswerRe: syncronisationmemberLiam Cavanagh4 Jul '07 - 2:59 
GeneralconnectionmemberMohammed.Reda8 Jun '07 - 6:42 
AnswerRe: connectionmemberLiam Cavanagh8 Jun '07 - 7:19 
GeneralRe: connectionmemberMohsen_Emgineer200411 Jun '07 - 13:47 
GeneralRe: connectionmemberLiam Cavanagh12 Jun '07 - 3:58 
GeneralRe: connectionmemberMohsen_Emgineer200412 Jun '07 - 16:31 
GeneralRe: connectionmemberLiam Cavanagh13 Jun '07 - 2:14 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 25 Feb 2008
Article Copyright 2007 by Liam Cavanagh
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid