Monitor network connectivity in WP7 apps

Posted by Daniel Moth on Daniel Moth See other posts from Daniel Moth or by Daniel Moth
Published on Fri, 27 May 2011 23:32:35 GMT Indexed on 2011/06/20 16:35 UTC
Read the original article Hit count: 324

Filed under:

Most interesting Windows Phone apps rely on some network service for their functionality. So at some point in your app you may need to know programmatically if there is network connection available or not.

For example, the Translator by Moth app relies on the Bing Translation service for translations. When a request for translation (text or voice) is made, the network call may fail. The failure reason is not evident from any of the return results, so I check the connection to see if it is present. Dependent on that, a different message is shown to the user. Before the translation phase is even reached, at the app start up time the Bing service is queried for its list of  languages; in that case I don't want to show the user a message and instead want to be notified when the network is available in order to send the query out again.

So for those two requirements (which I imagine are common in other apps) I wrote a simple wrapper MyNetwork static class to the framework APIs:

  1. Call once MyNetwork.MonitorNetworkAvailability() method so it monitors the network change
  2. At any time check the MyNetwork.IsConnected property to check for network presence
  3. Subscribe to its MyNetwork.ConnectionEstablished event
  4. Optionally, during debugging use its MyNetwork.ChangeStatus method to simulate a change in network status

As usual, there may be better ways to achieve this, but this class works perfectly for my scenarios. You can download the code here: MyNetworks.cs.




Comments about this post welcome at the original blog.

© Daniel Moth or respective owner

Related posts about MobileAndEmbedded

  • Translator by Moth v2

    as seen on Daniel Moth - Search for 'Daniel Moth'
    If you are looking for the full manual for this Windows Phone app you can find it here: "Translator by Moth". While the manual has no images (just text), in this post I will share images and if you like them, go get "Translator by Moth" from the Windows Phone marketplace. open the app from… >>> More

  • Windows Phone 7 developer resources

    as seen on Daniel Moth - Search for 'Daniel Moth'
    Developers of Windows Mobile 6.x (and indeed Windows CE) applications still use the rich .NET Compact Framework 3.5 with Visual Studio 2008 for development. That is still a great platform and the Mobile Development Handbook is still a useful resource (if I may say so myself :-). The release of Windows… >>> More

  • ScrollViewer.EnsureVisible for Windows Phone

    as seen on Daniel Moth - Search for 'Daniel Moth'
    In my Translator By Moth app, on both the current and saved pivot pages the need arose to programmatically scroll to the bottom. In the former, case it is when a translation takes place (if the text is too long, I want to scroll to the bottom of the translation so the user can focus on that, and not… >>> More

  • "Translator by Moth"

    as seen on Daniel Moth - Search for 'Daniel Moth'
    This article serves as the manual for the free Windows Phone 7 app called "Translator by Moth". The app is available from the following link (browse the link on your Window Phone 7 phone, or from your PC with zune software installed): http://social.zune.net/redirect?type=phoneApp&id=bcd09f8e-8211-e011-9264-00237de2db9e… >>> More

  • Watermark TextBox for Windows Phone

    as seen on Daniel Moth - Search for 'Daniel Moth'
    In my Translator by Moth app, in the textbox where the user enters a translation I show a "prompt" for the user that goes away when they tap on it to enter text (and returns if the textbox remains/becomes empty). See screenshot on the right (or download the free app to experience it). Back… >>> More