如何检查 Android 设备中存在的工作互联网?
How to check working internet present in Android Device?
正在通过以下方法检查互联网是否存在。
public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return (mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting());
} else
return false;}
但是当连接到 wifi 时,没有可用的互联网,那么此方法也适用。
提前致谢,请帮帮我
使用 obj.isAvailable()
检查网络可用性。
您可以在这里找到更多
NetworkInfo.
这是检查互联网的方式:-
100% 工作代码
private AlertDialog internetDialog, gpsAlertDialog;
public BroadcastReceiver internetConnectionReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo activeWIFIInfo = connectivityManager
.getNetworkInfo(connectivityManager.TYPE_WIFI);
if (activeWIFIInfo.isConnected() || activeNetInfo.isConnected()) {
removeInternetDialog();
} else {
if (isNetDialogShowing) {
return;
}
showInternetDialog();
}
}
};
这可能对你有帮助
public boolean isConnectingToInternet(){
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)
{
return true;
}
}
return false;
}
试试这个:
public static boolean Check_Internet_Connectivity(Context _context)
{
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)
{
return true;
}
}
}
Toast.makeText(_context, "Internet Connection is not available", Toast.LENGTH_SHORT).show();
return false;
}
Ping 任何真实主机(样本 8.8.8.8)
InetAddress.getByName("8.8.8.8").isReachable(timeOut)
如果你碰巧通过 webview 加载页面,你也可以检查它是否调用自定义 404 错误页面:
webView.loadUrl(urlhere);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/new404.html");
}
});
正在通过以下方法检查互联网是否存在。
public boolean isConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return (mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting());
} else
return false;}
但是当连接到 wifi 时,没有可用的互联网,那么此方法也适用。
提前致谢,请帮帮我
使用 obj.isAvailable()
检查网络可用性。
您可以在这里找到更多 NetworkInfo.
这是检查互联网的方式:-
100% 工作代码
private AlertDialog internetDialog, gpsAlertDialog;
public BroadcastReceiver internetConnectionReciever = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetInfo = connectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo activeWIFIInfo = connectivityManager
.getNetworkInfo(connectivityManager.TYPE_WIFI);
if (activeWIFIInfo.isConnected() || activeNetInfo.isConnected()) {
removeInternetDialog();
} else {
if (isNetDialogShowing) {
return;
}
showInternetDialog();
}
}
};
这可能对你有帮助
public boolean isConnectingToInternet(){
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)
{
return true;
}
}
return false;
}
试试这个:
public static boolean Check_Internet_Connectivity(Context _context)
{
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)
{
return true;
}
}
}
Toast.makeText(_context, "Internet Connection is not available", Toast.LENGTH_SHORT).show();
return false;
}
Ping 任何真实主机(样本 8.8.8.8)
InetAddress.getByName("8.8.8.8").isReachable(timeOut)
如果你碰巧通过 webview 加载页面,你也可以检查它是否调用自定义 404 错误页面:
webView.loadUrl(urlhere);
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/new404.html");
}
});