获取连接,即使该设备具有连接返回false [英] Getting connection returns false even when the device has connection

查看:538
本文介绍了获取连接,即使该设备具有连接返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了返回的位置,并将它发送到服务器上的alarmlistener,但firts它会检查互联网连接的应用程序。这是检查连接的方法:

I have an app that has an alarmlistener that returns position and send it to a server, but firts it checks the internet connection. This is the method that checks the connection:

public boolean hasInternetConnection() {        
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo networkInfo = connectivityManager
                    .getActiveNetworkInfo();            
            if (networkInfo != null && networkInfo.isAvailable()            
                    && networkInfo.isConnected()) {                     
                return true;
            } else {
                return false;                   
            }
        } else {
            return false;
        }
    }

在清单权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="pe.com.hacom.taxitrack.mgr.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

和它一直工作正常,但是当我试图在摩托罗拉XT316它不工作,它始终返回false,即使设备有联系,我检查它开扩的互联网浏览器,它显示了网页的正常,但报警仍返回false与方法。

And it has been working fine, but when I tried it on a Motorola XT316 it didn't work, it always return false, even when the device has connection, I check it openning the internet browser and it shows the webpage normally, but the alarm still returning false with that method.

我测试了它在其它设备上工作得很好用,而是摩托罗拉,任何想法发生了什么?是硬件错误?

I tested it in other devices and it works fine with all, but that Motorola, any idea what is happening? is that a hardware bug?

和最难得的是,有时我可以得到DDMS的设备上的调试日志,但几乎总是出现脱机。
我已经安装了在设备的驱动程序,还卸载它,并从网页安装摩托罗拉的通用驱动程序,以及它与Motoroal RAZR的但不能与XT316的工作。

And the most rare thing is that sometimes I can get the device in DDMS for the logs on the debug but almost always it appears "Offline". I have installed the driver that is in the device, also uninstalling it and installing the universal driver of motorola from the webpage, and its working with Motoroal RAZR's but not with the XT316.

推荐答案

我测试了很多,我发现,当我问多次为连接NetworkState是连接,让我改变,我问的方法连接。

I was testing a lot and I found that many times when I'm asking for connection the NetworkState is "Connecting" so I change the method where I ask for connection.

public boolean hasInternetConnection() {        
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo networkInfo = connectivityManager
                    .getActiveNetworkInfo();            
            if (networkInfo != null && networkInfo.isConnectedOrConnecting())
                {                     
                    return true;
                } else {
                    return false;                   
                }
        } else {
            return false;
        }
    }

我读谷歌的开发者页面上的文档,发现这是不推荐使用的ConnectedOrConnecting()方法,如果你发送或从互联网上recieving数据,但该方法我觉得有些设备的BatteryManagment可能下调也许它不会被连接起来的连接或者由于3G网络在我的城市很烂。

I read the documentation on google's developer page and found that the method that is not recommended to use the is ConnectedOrConnecting() method if you're sending or recieving data from internet, but I think that the BatteryManagment of some devices maybe cut the connection or maybe it doesn't get connected because the 3G network in my city sucks.

我知道这是不是最好的做法,但工作,它的奇怪发送,甚至recieving数据时,网络状态是连接。

I know this is not the best practice, but is working, and it's strangely sending and recieving data even when the network state is "connecting".

这篇关于获取连接,即使该设备具有连接返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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