欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 判斷是否能真正上網(wǎng)的實(shí)例詳解

 更新時(shí)間:2017年10月09日 15:20:27   作者:Alien__Chao  
這篇文章主要介紹了Android 判斷是否能真正上網(wǎng)的實(shí)例詳解相關(guān)資料,希望通過(guò)本文大家能夠掌握判斷是否上網(wǎng)的方法,需要的朋友可以參考下

Android 判斷是否能真正上網(wǎng)的實(shí)例詳解

 檢測(cè)網(wǎng)絡(luò)是否連接

實(shí)現(xiàn)代碼:

/**

 *
 檢測(cè)網(wǎng)絡(luò)是否連接

 *

 *
 @return

 */

private

boolean 
isNetworkAvailable() {

  //
 得到網(wǎng)絡(luò)連接信息

  ConnectivityManager
 manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

  //
 去進(jìn)行判斷網(wǎng)絡(luò)是否連接

  if

(manager.getActiveNetworkInfo() != null)
 {

    return

manager.getActiveNetworkInfo().isAvailable();

  }

  return

false;

}

 ping的狀態(tài)

public static final boolean ping() {

  String result = null;
  try {
    String ip = "www.baidu.com";// ping 的地址,可以換成任何一種可靠的外網(wǎng)
    Process p = Runtime.getRuntime().exec("ping -c 3 -w 100 " + ip);// ping網(wǎng)址3次
    // 讀取ping的內(nèi)容,可以不加
    InputStream input = p.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(input));
    StringBuffer stringBuffer = new StringBuffer();
    String content = "";
    while ((content = in.readLine()) != null) {
      stringBuffer.append(content);
    }
    Log.d("------ping-----", "result content : " + stringBuffer.toString());
    // ping的狀態(tài)
    int status = p.waitFor();
    if (status == 0) {
      result = "success";
      return true;
    } else {
      result = "failed";
    }
  } catch (IOException e) {
    result = "IOException";
  } catch (InterruptedException e) {
    result = "InterruptedException";
  } finally {
    Log.d("----result---", "result = " + result);
  }
  return false;

}

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

最新評(píng)論