android wifi 状态检测,Android系统下检测Wifi连接互联网是否正常的代码

该代码段包含三个方法,用于检查设备的网络连接状态、验证HTTP连接的可用性和进行字符编码转换。`isConnectInternet()`函数判断网络是否连接,`checkInternetConnection()`通过HTTP GET请求确认网络是否能正常访问指定URL,`big52unicode()`和`unicode2big5()`分别用于BIG5到UTF-8及UTF-8到BIG5的编码转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**

*

* 判断网络状态是否可用

*

* @return true: 网络可用 ; false: 网络不可用

*/

public boolean isConnectInternet()

{

ConnectivityManager conManager = (ConnectivityManager) test.this

.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo networkInfo = conManager.getActiveNetworkInfo();

if (networkInfo == null || !networkInfo.isConnected())

{

return false;

}

if (networkInfo.isConnected())

{

return true;

}

return false;

}

/* 检查网络联机是否正常 */

public boolean checkInternetConnection(String strURL, String strEncoding)

{

/* 最多延时n秒若无响应则表示无法联机 */

int intTimeout = 10;

try

{

HttpURLConnection urlConnection = null;

URL url = new URL(strURL);

urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.setRequestMethod("GET");

urlConnection.setDoOutput(true);

urlConnection.setDoInput(true);

urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0"

+ " (compatible; MSIE 6.0; Windows 2000)");

urlConnection.setRequestProperty("Content-type",

"text/html; charset=" + strEncoding);

urlConnection.setConnectTimeout(1000 * intTimeout);

urlConnection.connect();

if (urlConnection.getResponseCode() == 200)

{

return true;

}

else

{

Log.d("getResponseCode=", urlConnection.getResponseMessage());

return false;

}

}

catch (Exception e)

{

e.printStackTrace();

Log.d("emessage", e.getMessage());

return false;

}

}

/* 自定义BIG5转UTF-8 */

public String big52unicode(String strBIG5)

{

String strReturn = "";

try

{

strReturn = new String(strBIG5.getBytes("big5"), "UTF-8");

}

catch (Exception e)

{

e.printStackTrace();

}

return strReturn;

}

/* 自定义UTF-8转BIG5 */

public String unicode2big5(String strUTF8)

{

String strReturn = "";

try

{

strReturn = new String(strUTF8.getBytes("UTF-8"), "big5");

}

catch (Exception e)

{

e.printStackTrace();

}

return strReturn;

}

标签:

网络

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点!

本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值