安卓:检查互联网连接 - 返回true(错误地) [英] Android: Check internet connection - returns true (incorrectly)

查看:164
本文介绍了安卓:检查互联网连接 - 返回true(错误地)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要检查是否有互联网连接,我用这code:

To check for internet connectivity, i am using this code:

public boolean isOnline() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        //return netInfo != null && netInfo.isConnectedOrConnecting();
        return netInfo != null && netInfo.isAvailable() && netInfo.isConnected();
    }

这是最常用的$ C C I随处可见 - 但它是不可靠的工作结果。

This is the most commonly used code i found everywhere - however it is not working reliably.

这是返回true,虽然我无法使用互联网。结果
我的手机的其他应用程序正确地给出信息:无法连接到互联网

This is returning true, although i am unable to use internet.
Other apps on my mobile correctly give message: 'Can't connect to internet'.

修改1 结果
看来,它在互联网连接有返回true - 即使它是不可用的某些原因。结果
如果我关掉手机数据/互联网我的手机上 - 那么此方法正确返回false。

EDIT 1
It appears that it returns true when internet connection is there - even though it is unusable for some reason.
If i switch off the Mobile Data/Internet on my phone - then this method correctly returns false.

推荐答案

这些都将会告诉你,如果你的设备连接与否,他们不告诉你在互联网的地位......结果
对于您应该使用这种code ...

Those all will tell you if your device is connected or not, they do not tell you the internet status...
For that you should use this code...

 private boolean isOnline() {
            try{
                // ping to googlevto check internet connectivity
                Socket socket = new Socket();
                SocketAddress socketAddress = new InetSocketAddress("8.8.8.8", 80);
                socket.connect(socketAddress, 3000);
                socket.close();
                return true;
            } catch (Exception e) {
                 // internet not working
                 return false
            }
 }

您必须异步任务,做到这一点还是它会给你网络上的主线程例外......结果
我假设你已经在异步调用任务的方法isOnline

You must do this in async task or it will give you network on main thread exception...
I am assuming you are already calling isOnline method in async task.

那么,如果你不是在一个异步任务使用这个,结果
你必须在这里使用它像这样....

Well if you are not using this in an async task,
You must use it here like this....

比如你想去某某活动仅在互联网上公布

for example you want to go to xyz activity only if internet available

private class GoToXYZActivity extends AsyncTask<String, Void, Void> {
     boolean internetAvailable;
     protected String doInBackground(String... urls) {
         //THIS METHOD WILL BE CALLED AFTER ONPREEXECUTE
         //YOUR NETWORK OPERATION HERE
         internetAvailable = inOnline();
         return null;
     }

     protected void onPreExecute() {
         super.onPreExecute();
         //THIS METHOD WILL BE CALLED FIRST
         //DO OPERATION LIKE SHOWING PROGRESS DIALOG PRIOR TO BEGIN NETWORK OPERATION
     }

     protected void onPostExecute(String result) {
         super.onPostExecute();
         //TNIS METHOD WILL BE CALLED AT LAST AFTER DOINBACKGROUND
         //DO OPERATION LIKE UPDATING UI HERE
         if (internetAvailable)
             /// goto xyz activity
         else
             /// toast - no internet
     }
 }

和上点击事件,你必须调用此方法

And on click event you must call this method

 new GoToXYZActivity().execute();

这篇关于安卓:检查互联网连接 - 返回true(错误地)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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