Skip to content Skip to sidebar Skip to footer

How To Check Internet Access (including Tethering!) On Android?

I am looking for a piece of code that would check network connectivity, of an Android device, including tethering, not only the wireless channels. I have spent hours online lookin

Solution 1:

this checks if Tethering is enabled. Cut from a class I changed to get adbWireless working over a tethered connection:

// check if tethering is ontry {
        Method[] wmMethods = mWifiManager.getClass().getDeclaredMethods();
        for(Method method: wmMethods)
            if("isWifiApEnabled".equals(method.getName()))
                return (Boolean) method.invoke(mWifiManager);
        returnfalse;
    } catch (Exception x) {returnfalse;}

And you also need android.permission.ACCESS_WIFI_STATE

Solution 2:

Check this out:

publicstaticbooleanisOnline(Context context) {
    booleanisOnline=false;
    try {
        finalConnectivityManagerconMgr= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        finalNetworkInfoactiveNetwork= conMgr.getActiveNetworkInfo();
        if(activeNetwork != null && activeNetwork.isConnected() && activeNetwork.isAvailable()) {
            isOnline = true;
        } else {
            isOnline = false;
        } 
    } catch(Exception e) {
        Log.e(Config.LOG_TAG, e.toString());
        isOnline = false;
    }
    return isOnline;
}

Also add such permission into AndroidManifest.xml:

<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE" />

Post a Comment for "How To Check Internet Access (including Tethering!) On Android?"