如何在连接wifi时检查互联网接入? [英] How to check internet access when wifi is connected?

查看:175
本文介绍了如何在连接wifi时检查互联网接入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何检查互联网接入互联网接入或没有互联网接入。如果无线连接或不接通



我尝试过:



  public   boolean  checkOnlineState(){
ConnectivityManager CManager =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo NInfo = CManager.getActiveNetworkInfo();
if (NInfo!= null&& NInfo.isConnectedOrConnecting()){
try {
if (InetAddress.getByName( https://www.google.fr/)。isReachable( 10000 ))
{
< span class =code-comment> // 主机可访问
返回真正;
}
其他
{
// < span class =code-comment>主机无法访问
return false;
}
} catch (IOException e){
e.printStackTrace();
}
}
return false;
}



申请立即停止!!任何帮助?

解决方案

 isConnectedOrConnecting()

不够精确它应该

 isConnected()





见这里: Android检查互联网连接 - 堆栈溢出 [ ^ ]


使用此解决方案代码您的应用程序不会崩溃:



< pre lang =C#> public boolean checkInternetConnectionALL(){
ConnectivityManager conmag =(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);所有有效网络连接的
//

NetworkInfo info = conmag.getActiveNetworkInfo();
if (info!= null && info.isConnected())
返回 true ;
else
Toast.makeText(context.getApplicationContext(), No Internet Connection,Toast.LENGTH_LONG)。show();

return false ;
}


how i can check the internet access internet access or no internet access.not if wifi connected or not

What I have tried:

public boolean checkOnlineState() {
        ConnectivityManager CManager =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo NInfo = CManager.getActiveNetworkInfo();
        if (NInfo != null && NInfo.isConnectedOrConnecting()) {
            try {
                if (InetAddress.getByName("https://www.google.fr/").isReachable(10000))
                {
                    // host reachable
                    return true;
                }
                else
                {
                    // host not reachable
                    return false;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }


the application stopped immediately!! any help ?

解决方案

isConnectedOrConnecting()

is not precise enough it should be

isConnected()



See here: Android check internet connection - Stack Overflow[^]


use this solution code your app not crash:

public boolean checkInternetConnectionALL() {
       ConnectivityManager conmag = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
       // for all active Network connection

       NetworkInfo info = conmag.getActiveNetworkInfo();
       if (info != null && info.isConnected())
           return true;
       else
           Toast.makeText(context.getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG).show();

       return false;
   }


这篇关于如何在连接wifi时检查互联网接入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆