我想检查设备中是否有可用的Internet连接,所以我怎么能得到这个..请给我这个代码..
非常感谢提前
答案 0 :(得分:3)
嘿伙计...应用此代码..这可能对你提前帮助....
布尔连接;
private boolean checkInternetConnection()
{
ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
// ARE WE CONNECTED TO THE NET
if (conMgr.getActiveNetworkInfo() != null
&& conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected())
{
return true;
}
else
{
return false;
}
}
答案 1 :(得分:2)
ConnectivityManager connectionService =
(ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if (connectionService.getActiveNetworkInfo().isConnectedOrConnecting()) {
// Device is online
}
答案 2 :(得分:1)
使用以下功能检查互联网连接:
public boolean isOnline()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
boolean result = false;
if(ni != null )
{
if( ni.getState() == NetworkInfo.State.CONNECTED )
{
result = true;
}
}
return result;
}
检查 isOnline()返回的内容。如果为true则会连接Internet,否则Inernet未连接。 希望这会对你有所帮助.. :)
答案 3 :(得分:1)
// Network is connected or not
public boolean isConnected()
{
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
Log.d("LOG","Network is Available");
return true;
}
}
return false;
}
答案 4 :(得分:0)
使用此答案帮助。
public boolean isOnline() {
ConnectivityManager cm =(ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
if(temp==true){
genHelper.showToast("Net Connection");
}else{
genHelper.showToast(" Not Connected");
}