检测互联网连接android

时间:2015-11-06 08:01:33

标签: android

我一直在使用 ConnectivityManager 来检测是否存在互联网连接,但它只会告诉您无论是否确实存在互联网连接 ,我听说通过发送ping 可以检测到https://www.google.com/。我还要展示祝酒词。

ConnectivityManager cmanager =     (ConnectivityManager)getSystemService(this.CONNECTIVITY_SERVICE);
    NetworkInfo info = cmanager.getActiveNetworkInfo();
    if(info!=null && info.isConnected()) {
        if(info.getType() == ConnectivityManager.TYPE_WIFI) {
            Toast.makeText(MainActivity.this, "Wifi", Toast.LENGTH_LONG).show();   
        } else if(info.getType() == ConnectivityManager.TYPE_MOBILE) {
            Toast.makeText(MainActivity.this, "mobile", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(MainActivity.this, "Not connected", Toast.LENGTH_LONG).show();
    }
}


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

4 个答案:

答案 0 :(得分:0)

我希望这会对你有所帮助。

public static boolean thereIsConnection(Context context) {

    if (SystemHelper.isEmulator(context)) {
        return true;
    }

    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo i = conMgr.getActiveNetworkInfo();
    if (i == null) {
        return false;
    }
    if (!i.isConnected()) {
        return false;
    }
    if (!i.isAvailable()) {
        return false;
    }
    return true;
}

答案 1 :(得分:0)

非常简单:

private boolean haveInternet(){
NetworkInfo info = ((ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info==null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to disable internet while roaming, just return false
return true;
}
return true;
}

答案 2 :(得分:0)

根据您的问题尝试:

    public static boolean hasActiveInternetConnection(Context context) {
        if (isNetworkAvailable(context)) {
            try {
                HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
                urlc.setRequestProperty("User-Agent", "Test");
                urlc.setRequestProperty("Connection", "close");
                urlc.setConnectTimeout(1500); 
                urlc.connect();
                return (urlc.getResponseCode() == 200);
            } catch (IOException e) {
                Log.e("Connection Status:", "Error checking internet connection", e);
            }
        } else {
            Log.e("Connection Status:", "No network available!");
        }
        return false;
    }

不要忘记提供所需的权限。

&安培;不要在主线程上运行它(去Asynctask这样的后台线程)。

答案 3 :(得分:0)

是的,你应该通过ping来做到这一点。这是我的网络工具正常工作:

    public class NetworkUtil {

        Context context;

        public static boolean isConnected(Context context) {
            return isNetworkAvailable();
        }

        private static boolean isNetworkAvailable() {ConnectivityManager connectivityManager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        if(info!=null && info.isConnected() && canAccessToInternet()) {
            if(info.getType() == ConnectivityManager.TYPE_WIFI) {
                Toast.makeText(MainActivity.this, "Wifi", Toast.LENGTH_LONG).show();   
            } else if(info.getType() == ConnectivityManager.TYPE_MOBILE) {
                Toast.makeText(MainActivity.this, "mobile", Toast.LENGTH_LONG).show();
            }
            return true;
        } else {
            Toast.makeText(MainActivity.this, "Not connected", Toast.LENGTH_LONG).show();
            return false;    
        }
    }

    private static Boolean canAccessToInternet() {
        Runtime runtime = Runtime.getRuntime();
        try {
            Process ipProcess = runtime.exec("/system/bin/ping -c 1 google.com");
            int exitValue = ipProcess.waitFor();
            return exitValue == 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}