Reputation: 13
I have an app that uploads multimedia files to server by using DefaultHttpClient. But I would like only to restrict this upload capability to Wifi only. I have tried using ConnectivityManager but it affects my phone settings.
How do I write this in my code so that this settings only applies to my app and does not affect my phone settings? Thanks in advance.
Upvotes: 0
Views: 198
Reputation: 28823
Try this:
ConnectivityManager conManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo nwInfo = conManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (nwInfo.isConnected()) {
uploadFilesToServer(); //Your function
}
Hope it helps.
Upvotes: 0
Reputation: 5542
public class NetworkConnection
{
public static boolean isConnected(Context context)
{
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = null;
if (connectivityManager != null) {
networkInfo =
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
}
boolean b = networkInfo.isConnected();
boolean result=(b);
return (networkInfo == null) ? false : result;
}
}
then you can call this class anywhere you want by :
if(NetworkConnection.isConnected(YourActivity.this))
{
//Wi-fi enable
}else{
//Wi-fi disable
}
Upvotes: 3
Reputation: 14710
Using WifiManager class, you could check if wi-fi is enabled:
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi.isWifiEnabled()){
//wifi is enabled
}
and if so, perform the upload. I am not sure what you mean with tried using ConnectivityManager but it affects my phone settings
Upvotes: 0