[英]android: how to know internet connection is available or not in device?
I want to check the Internet connection available or not in device so how can i got this..please give me code for this.. 我想检查设备中的互联网连接是否可用,所以我该如何获取..请为此提供代码。
thank a lot In advance 非常感谢
hey buddy...apply this code..it might be helpful to u.thanks in advance.... 嘿,伙计...应用此代码..可能对提前感谢您很有帮助....
boolean connected; 布尔连接;
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;
}
}
ConnectivityManager connectionService =
(ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
if (connectionService.getActiveNetworkInfo().isConnectedOrConnecting()) {
// Device is online
}
// 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;
}
Use the following function inorder to check the internet connction: 请使用以下功能为了检查互联网连接:
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;
}
check what isOnline() returns .If true then Internet is connected else Inernet is not connected . 检查isOnline()返回的结果。如果为true,则说明已连接Internet,否则未连接Inernet 。 Hope this will help you..:)
希望这个能对您有所帮助..:)
Use this Answer It`s help. 使用此Answer It的帮助。
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.