如何验证android活动是否存在互联网

如何验证android活动是否存在互联网,android,Android,当wifi关闭且移动网络关闭时,我可以检测到它,但当两者都打开时,即使没有网络,我的验证也无法工作。现在我需要知道,当移动数据和WIFI打开或关闭时,我如何验证互联网的缺失 我的代码: NetworkInfo nf; ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); nf=cn.getActiveNetworkInfo();

当wifi关闭且移动网络关闭时,我可以检测到它,但当两者都打开时,即使没有网络,我的验证也无法工作。现在我需要知道,当移动数据和WIFI打开或关闭时,我如何验证互联网的缺失

我的代码:

NetworkInfo nf;


 ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    nf=cn.getActiveNetworkInfo();

             if(nf != null && nf.isConnected()==true )  
             {

        //    switch to new activity
             }

你可以尝试ping google.com来验证你的连接。这里有堆栈溢出主题:

您可以尝试ping google.com来验证您的连接。这里有一个堆栈溢出主题:

我正在使用它,到目前为止它工作正常

   public static boolean isOnline() {
        InetAddress inetAddress = null;
        try {
            Future<InetAddress> future = Executors.newSingleThreadExecutor().submit(new Callable<InetAddress>() {
                @Override
                public InetAddress call() {
                    try {
                        return InetAddress.getByName("google.com");
                    } catch (UnknownHostException e) {
                        return null;
                    }
                }
            });
            inetAddress = future.get(2000, TimeUnit.MILLISECONDS);
            future.cancel(true);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return inetAddress != null && !inetAddress.equals("");
    }
publicstaticbooleanisonline(){
InetAddress InetAddress=null;
试一试{
Future Future=Executors.newSingleThreadExecutor().submit(new Callable()){
@凌驾
公共广播呼叫(){
试一试{
返回InetAddress.getByName(“google.com”);
}捕获(未知后异常e){
返回null;
}
}
});
inetAddress=future.get(2000,时间单位为毫秒);
future.cancel(true);
}捕获(中断异常|执行异常e){
e、 printStackTrace();
}捕获(超时异常e){
e、 printStackTrace();
}
返回inetAddress!=null&!inetAddress.equals(“”);
}

您可以将它放在
NetworkUtil
类中,并使用静态方法检查连接。

我正在使用它,目前为止它工作正常

   public static boolean isOnline() {
        InetAddress inetAddress = null;
        try {
            Future<InetAddress> future = Executors.newSingleThreadExecutor().submit(new Callable<InetAddress>() {
                @Override
                public InetAddress call() {
                    try {
                        return InetAddress.getByName("google.com");
                    } catch (UnknownHostException e) {
                        return null;
                    }
                }
            });
            inetAddress = future.get(2000, TimeUnit.MILLISECONDS);
            future.cancel(true);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        return inetAddress != null && !inetAddress.equals("");
    }
publicstaticbooleanisonline(){
InetAddress InetAddress=null;
试一试{
Future Future=Executors.newSingleThreadExecutor().submit(new Callable()){
@凌驾
公共广播呼叫(){
试一试{
返回InetAddress.getByName(“google.com”);
}捕获(未知后异常e){
返回null;
}
}
});
inetAddress=future.get(2000,时间单位为毫秒);
future.cancel(true);
}捕获(中断异常|执行异常e){
e、 printStackTrace();
}捕获(超时异常e){
e、 printStackTrace();
}
返回inetAddress!=null&!inetAddress.equals(“”);
}

您可以将其放入
NetworkUtil
类中,并使用静态方法检查连接。

我不知道您的应用程序是否属于“连接检查应用程序”或其他类型,但对于普通应用程序,我建议应用程序的“任务”不是测试是否存在互联网连接。暂停并相应地处理这个案例,因为你无论如何都要处理它。@JacksOnF1re你将负责检查现有的internetWell我不知道你的应用程序是“连接检查应用程序”还是其他什么,但对于一个普通的应用程序,我建议它不是“工作”要测试是否存在internet连接的应用程序的。暂停并相应地处理这个问题,因为你无论如何都要处理它。@JacksOnF1re负责检查现有互联网的是同一个问题,当我关闭移动数据或wifi时,它工作正常,但,即使wifi或移动数据丢失,代码也无法检测是否缺少互联网on@user3518835如何检查除了此处的代码之外是否缺少internet?
if(isOnline){//转到活动二}否则{//Toast..“需要internet”}用于wifi的公共静态布尔isOnline(){..}
现在是超级,但我不知道为什么移动网络在没有互联网的情况下,在移动数据打开的情况下尝试切换到活动2。这是同一个问题,当我关闭移动数据或wifi时,它工作正常,但,即使wifi或移动数据丢失,代码也无法检测是否缺少互联网on@user3518835如何检查除了此处的代码之外是否缺少internet?
if(isOnline){//转到活动二}否则{//Toast..“需要internet”}用于wifi的公共静态布尔isOnline(){..}
现在是超级,但我不知道为什么移动网络在移动数据打开的情况下尝试切换到活动二,即使我没有互联网。