t’s more than likely that a Windows Phone app will need to work with data that resides on the Internet. For example, an app may aggregate data from various news sources using RSS feeds. Consuming this type of data in a Windows Phone app is very similar to consuming it in an Android app. In this post I’ll show you how to manage network usage.
It’s important to consider how your Windows Phone app will access network resources once it has the capability to do so. If your app is going to perform a large number of network operations or transfer large amounts of data (e.g. audio/video streaming) you should allow the user to control when these operations are performed.
In a Windows Phone app you can put the user in control over network usage in four steps. First, create an enumeration representing various states of connectivity:
public
enum ConnectionState
2.
{
3.
NotConnected,
4.
WiFi,
5.
Ethernet,
6.
Cellular,
7.
Unknown
8.
}
Second, create a method that leverages the NetworkInterface class to determine if the device the app is running on is connected to a network and, if the device is connected, what kind of network the device is on. The method could look like the following:
01.
private
ConnectionState GetConnectionState()
02.
{
03.
switch
(Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType)
04.
{
05.
case
NetworkInterfaceType.None:
06.
return
ConnectionState.NotConnected;
07.
08.
case
NetworkInterfaceType.Ethernet:
09.
return
ConnectionState.Ethernet;
10.
11.
case
NetworkInterfaceType.Wireless80211:
12.
return
ConnectionState.WiFi;
13.
14.
case
NetworkInterfaceType.MobileBroadbandCdma:
15.
case
NetworkInterfaceType.MobileBroadbandGsm:
16.
return
ConnectionState.Cellular;
17.
18.
default
:
19.
return
ConnectionState.Unknown;
20.
}
21.
}
Third, add a method to your class that will perform an operation based on the connection state:
1.
private
void
UpdateActivityBasedOnConnectionState()
2.
{
3.
var
connectionState = GetConnectionState();
4.
//Perform some action based on state (e.g. start/stop streaming media, prompt user, etc.)
5.
}
Finally, in the constructor of your class add an event handler for theNetworkChange.NetworkAddressChanged event. This event fires when the network address changes for a connection. This will allow you to know if a connection has been lost, or if the device has switched to a different type of connection.
01.
public
NetworkManager()
02.
{
03.
UpdateActivityBasedOnConnectionState();
04.
NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
05.
}
06.
07.
private
void
NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
08.
{
09.
UpdateActivityBasedOnConnectionState();
10.
}
Android tip |
To obtain information about the network(s) a device is connected to in Android you would need to declare the following manifest permission:1. <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" />
You would then use the ConnectivityManager and NetworkInfo classes to determine what type of network the device is connected to. The following code shows how to determine if a device is connected to a Wi-Fi or mobile network:
01. private boolean IsWifiConnection(){ 02. ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 03. 04. NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 05. 06. return networkInfo.isConnected(); 07. } 08. 09. private boolean IsMobileConnection(){ 10. ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 11. 12. NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 13. 14. return networkInfo.isConnected() 15. } |
No comments:
Post a Comment