nr1
nr1

Reputation: 133

Android: How to perform Url Request via Wifi

is there a possiblity to explicitely use the wifi connection for doing Http Url requests? In fact i just need to know if an internet connection (access to google.com for example) is possible via wifi. (not via 2g / 3g / ..)

Upvotes: 0

Views: 867

Answers (3)

blindstuff
blindstuff

Reputation: 18348

I havent done this, so i'm not sure, but I found this looking through the documentation.

public boolean wifiAvailable() {
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connMgr.getActiveNetworkInfo();
        if((info.isAvailable() && info.isConnected() && (info.getType()==ConnectivityManager.TYPE_WIFI))) return true;
        return false;
    }

Where you are going to fire a request, evaluate this method, if it returns true the system will automatically use WiFi, android will always use wifi over 3G/2G when available AFAIK.

Upvotes: 1

Vit Khudenko
Vit Khudenko

Reputation: 28418

You may find this useful:

public class ConnectivityHelper {

    public static boolean isWiFiNetworkConnected(Context context) {
        return getWiFiNetworkInfo(context).isConnected();
    }

    private static NetworkInfo getWiFiNetworkInfo(Context context) {
        return getConnectivityManager(context).getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    }

    private static ConnectivityManager getConnectivityManager(Context context) {
        return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    }
}

Upvotes: 2

Aliostad
Aliostad

Reputation: 81660

As far as I know, no. This is all abstracted from us by the platform.

But you can check to see if WIFI is available:

        ConnectivityManager conMan = (ConnectivityManager) 
            getSystemService(Context.CONNECTIVITY_SERVICE);
        if(conMan!=null){
            try {
                NetworkInfo[] networkInfos = conMan.getAllNetworkInfo();
                for(NetworkInfo ni : networkInfos){
                    if(ni.isAvailable() && ni.isConnected() && ni.getTypeName().equals("WIFI")){
                        result = true;
                        break;
                    }
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }               
        }

Upvotes: 2

Related Questions