Android系统下检测Wifi连接互联网是否正常的代码
发布时间:2020-05-24 20:01:32 所属栏目:Java 来源:互联网
导读:Android系统下检测Wifi连接互联网是否正常的代码
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 /**
*
* 判断网络状态是否可用
*
* @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;
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
