Android to determine if you can actually surf the internet

Source: Internet
Author: User

Android to determine whether you can surf the internet, commonly used is the following method:

?
1234567891011121314 /** * 检测网络是否连接 *  * @return */private boolean isNetworkAvailable() {    // 得到网络连接信息    ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);    // 去进行判断网络是否连接    if (manager.getActiveNetworkInfo() != null) {        return manager.getActiveNetworkInfo().isAvailable();    }    return false;}

However, sometimes we connect to a network that does not have an extranet connection or need to enter an account and password to link to an extranet, which can occur although the network is available, but the extranet is not accessible. In response to this situation, the general solution is to ping an external network, if you can ping the instructions can really surf the internet, the code is as follows

?
1234567891011121314151617181920212223242526272829303132333435 @authorsichard     @category 判断是否有外网连接(普通方法不能判断外网的网络是否连接,比如连接上局域网)     @return     */     publicstaticfinalbooleanping() {              String result = null        try                String ip = "www.baidu.com";// ping 的地址,可以换成任何一种可靠的外网                 Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 "+ ip);// ping网址3次                 // 读取ping的内容,可以不加                 InputStream input = p.getInputStream();                 BufferedReader in = newBufferedReader(newInputStreamReader(input));                 StringBuffer stringBuffer = newStringBuffer();                 String content = ""                while((content = in.readLine()) != null) {                         stringBuffer.append(content);                                 Log.d("------ping-----""result content : "+ stringBuffer.toString());                 // ping的状态                 intstatus = p.waitFor();                 if(status == 0) {                         result = "success"                        returntrue                else                        result = "failed"                        catch(IOException e) {                 result = "IOException"        catch(InterruptedException e) {                 result = "InterruptedException"        finally                Log.d("----result---""result = "+ result);                 returnfalse;

Push family, free tickets, scenic spots: www.tuituizu.com

A companion tour, a free dating site: www.jieberu.com

Android to determine if you can really surf the internet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.