Monitor network connectivity in WP7 apps

Fri, May 27, 2011, 04:32 PM under MobileAndEmbedded

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.

Tuesday, July 12, 2011 5:43:33 AM (Pacific Daylight Time, UTC-07:00)
Thanks for the share !
Wednesday, July 13, 2011 8:32:18 AM (Pacific Daylight Time, UTC-07:00)
I love your blog, coming on my bookmarks
Comments are closed.