Search code examples
androidwificommunication3grestriction

Wifi communication only


I need to communicate over htttp or https from an android, and I need a settings for my app "wifi only". It's a restriction for the communication. How to implement such a thing? I cannot find any information on AndroidHttpClient works only with Wifi - or 3G as well, and how to restrict it.


Solution

  • This code will check for wifi connection.

     public static boolean hasWIFIConnection(Context context)
                        {
                            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    
                            NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                            if (wifiNetwork != null && wifiNetwork.isConnected())
                                {
                                    return true;
                                }
    return false;
    
            }