user2720184
user2720184

Reputation: 13

Set connectivity settings to android app

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

Answers (3)

MysticMagicϡ
MysticMagicϡ

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

Suhail Mehta
Suhail Mehta

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

gunar
gunar

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

Related Questions