Reputation: 15734
My app checks for Network connection when starting. In Android versions 2.3 thru 4.4 the below method works great (whether emulator or actual device). On the Android L Preview Emulator, the method comes back false.
Here is my code:
// ===============================================================
public static boolean haveNetworkConnection(Context ctx) {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
boolean haveConnectedEthernet = false;
ConnectivityManager cm = (ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
// String con = ni.isConnected() ? "Connected" : "Not Connected";
if (!haveConnectedWifi) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
}
if (!haveConnectedMobile) {
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
if (!haveConnectedEthernet) {
if (ni.getTypeName().equalsIgnoreCase("ETHERNET"))
if (ni.isConnected())
haveConnectedEthernet = true;
}
StringBuilder sb = new StringBuilder();
sb.append(ni.getTypeName()).append(" ").append(ni);
}
return haveConnectedWifi || haveConnectedMobile
|| haveConnectedEthernet;
}
There is some weird StringBuilder
stuff, I did not create this code -- it is in a project I inherited. I have not needed to change anything since it works. Does anyone know if there is any API changes with how ConnectivityManager
works? I checked the API overview changes on the dev site but did not see any relevant info on this.
Upvotes: 0
Views: 393
Reputation: 768
Typename has been changed in L, for example should check "Cellular" instead of "MOBILE".
Upvotes: 1