I am not sure this problem belongs here.... but still ....
My application exchange's data with server every 3 minutes. I am using honeycomb tablet on cell phone coverage(not Wi-Fi). If a person is using it where there is no cell phone covearge he is not going to get new data. How do I deal with this situation? What do I do in the application?
Here this will allow you to test for WIFI and 3g/4g coverage:
private boolean hasNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
Related
The code i am using to check internet connectivity so far is
private boolean isOnline() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
my code is working fine till android 5.0 but in android 6.0 the app crashes can anyone help me regarding this issue and provide me a better code that can test internet connectivity
Thanks
Regards
getAllNetworkInfo is deprecated
you need to use: getActiveNetworkInfo()
code:
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) { // connected to the internet
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
// connected to wifi
Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
} else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
//connected to Data
Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
}
} else {
// not connected to the internet
}
From the ConnectivityManager documentation:
NetworkInfo[] getAllNetworkInfo ()
This method was deprecated in API level 23.
This method does not support multiple connected networks of the same type. Use getAllNetworks() and getNetworkInfo(android.net.Network) instead.
Bear in mind getAllNetworks() was introduced in API Level 21, so you may need to run two separate approaches based on the user's OS version.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
// Use getAllNetworks()
} else{
// Use getAllNetworkInfo()
}
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.
Typename has been changed in L, for example should check "Cellular" instead of "MOBILE".
I have implemented code for connect internet through WifiManager.java. but i am not able to find any api to connect internet through Ethernet. Can any one help me.
Not that I am aware of.
Here is a method that detected WIFI and Data separately.
Perhaps you can alter this and put a prompt in there forcing user to turn off data and turn on WIFI.
public static boolean isNetworkConnected(Context context) {
boolean isConnectedWifi = false;
boolean isConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
isConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
isConnectedMobile = true;
}
return isConnectedWifi || isConnectedMobile;
}
i used this code to check the internet connection
private boolean haveNetworkConnection() {
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
but it doesn"t handle the case when I have a limited connectivity to the internet, how to fix it ?
Try to open a web link programmatically in background if you get a success status then internet connection is available else if it fails there is no internet connection available. Try HttpURLConnection to check the internet availability by opening any link, you can get HttpURLConnection details in given link : HttpURLConnection
Whether or not the device has 3g/data activated. Any idea of what's happening?
Thank you
My code:
public boolean isConnected3G(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networks = cm.getAllNetworkInfo();
for (NetworkInfo ni : networks)
if ("MOBILE".equalsIgnoreCase(ni.getTypeName())){
Log.d(TAG,""+ni.isConnected());
if (ni.isConnected())
return true;
}
return false;
}
Try to use NetworkInfo ni = cm.getActiveNetworkInfo() instead of cm.getAllNetworkInfo();
and check the network information