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: 350
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:
- Call once MyNetwork.MonitorNetworkAvailability() method so it monitors the network change
- At any time check the MyNetwork.IsConnected property to check for network presence
- Subscribe to its MyNetwork.ConnectionEstablished event
- 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