Search code examples
androidwifisettingsconnectivity

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.


Solution

  •     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
    }