Aiapaec Aiapaec - 2 months ago 11
Android Question

Getting connection returns false even when the device has connection

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;
}
}


Permissions on the Manifest:

<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" />


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?

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.

Answer Source

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;
        }
    }

I read the documentation on Google's developer page and found that it is not recommended to use the isConnectedOrConnecting() method if you're sending or receiving data from the 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.

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