Android - no connectivity for my App. How to debug? - android

I have check for internet connectivity that goes like this:
public static boolean isInternetAvailable(Context ctx)
{
NetworkInfo info = ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info == null || !info.isConnected()) return false;
if (info.isRoaming())
{
return Preferences.getIsRoamingAllowed(ctx);
}
return true;
}
It's been working good for all installations so far. Today user came in with a phone where everything worked (browser, email, etc) and it wasn't roaming. But my app due to this check was giving "No connection" error.
Phone is HTC Droid ERIS with 2.1
Anyone saw this issue? Any pointers why?

Ok I have written a test application that gets the activenetwork and all networks and lets me see what is happening, I am about to go out and test this since the anomalies I am seeing happen when switching from one network to the other (as in when you go out of wifi range and into cdma etc)
Couple of things that might help regardless first you can change info.isConnected to the following
if (info == null || !info.isConnectedOrConnecting()) return false;
This makes it a little more robust in that if you are in the middle of switch over you still let the user logon
Second thing is that you said that your app denied login if roaming because your apps allow roaming preference was set to false
return Preferences.getIsRoamingAllowed(ctx);
I think you need to follow a different pattern (just my opinion) first because If the user has disallowed roaming via their settings (phone not your app) and they are on a roaming network then the .getActiveNetwork() will return null, or not connected or not available (in which case the .getReason returns "noroaming")
Personally I would let the phone decide, but if you need to restrict it then he pattern I would follow would be
Set the default to true, but note that it's the first time your activity has started and the user has had no chance to set anything (since no pref's are set this should be easy enough to detect) Detect your network connection, if you have one then also note if they are roaming
Prompt them with an alert dialog which warns them they are currently roaming and Ask them if they want to login now or wait until later
OR
Normal Login if they are not roaming
But in either case offer them the ability to set the "roaming" option the first time instead of having them figure it out themselves.
That would address your catch 22 situation and save you some phone calls, anyway that's a design decision but I think it would work out better for you than your current pattern.
Also I think instead of just telling them there is no connection you might want to tell them why, return an enum instead of a boolean and then format dependent on that.
Finally I am going to test a bit more before my final answer because I am seeing some oddity's in the network state but want to confirm my findings before giving you the results, I need this as well so it was a good time for me to dig into this.

Related

How to check mobile network state?

I'd like to be able to identify an unactivated/disconnected SIM card, i.e,
About phone>SIM status>Mobile Network State>"Disconnected"
Here are some of the things I've tried so far.
telephonyManager.simState == TelephonyManager.SIM_STATE_READY
returns true even in the disconnected scenario.
telephonyManager.dataState != TelephonyManager.DATA_DISCONNECTED
returns false even when mobile data is turned off in an active device.
Can any of the network capabilities (https://developer.android.com/reference/android/net/NetworkCapabilities#constants_1) used for this? Some indicate the connection between the carrier, but I'm unsure which one applies to this scenario.

CN1 Connectivity - Concerns when internet is unstable in Public places

I'm using Connectivity library to see the internet(Wifi or network data) is active and saving the data in Storage if there is no connectivity(Offline) and synchronize with server when connected to internet. I'm having issues in public places where the internet is consistently unstable(esp. in basements, offices, stores, Coffee shops etc., where there is internet connects in and out). When I check the Connectivity is active but by the time I started synchronizing internet goes offline (Something like this). this leads inconsistent /partial updates to the server. Also, in coffee shops and Airports where wifi gets connected but there will be "Agree Terms and Conditions" page to connect. Not all the browsers will take you to that page directly after joining the Wifi. In that case I see the wifi is active in Mobile but actually it is not activated until I accept the terms and Conditions in IE or some specific browser. Any one else having difficulty in handling these kind of issue from Mobile App?
My App - Hangs on Login screen if I'm trying to login when there is in-stable/in consistent internet.It thinks wifi is there but not.
IF I'm on a screen where I will display list, screen will show blank for infinite time. Adding timeout for server request/response or something will help such scenario.
I know I'm not handling this case in code to show some kind of error message but I need some guidance to detect these conditions suing CN1 API to handle through my app.Please advise.
Code:
public boolean isOffline() {
if (Connectivity.isConnected() && forceOffline) {
relogin();
}
return forceOffline || !Connectivity.isConnected();
}
The problem is that it's impossible to detect online/offline properly as you might be connected to a local wifi/network but it might be a bad connection that won't let you reach the server. As far as the library is concerned you are connected... But in reality you don't have a connection.
First set the timeout values in NetworkManager to lower values to improve the experience although this won't solve a situation where data starts downloading and stops in the middle.
Next you need to handle these cases one by one and provide the user with a way to toggle the offline mode. Unfortunately there is no silver bullet for network reliability. You just need to go through every path and try to detect these things.

Android Preferred Network List

I was under the impression that wifiManager.getConfiguredNetworks() returns the PNL of the current device, however when I click on - forget network, wifiManager.getConfiguredNetworks() still keeps that network but just removes the connection details(like security, password etc.)
Is there anyway knowing which SSID(or BSSID) is actually in PNL? (i.e my device can automatically connect to it)
I found out that this problem appeared only on specifics networks, while in others clicking on forget network actually removes the network from wifiManager.getConfiguredNetworks().
I also found out that the only things different are allowedKeyManagement.cardinality and LinkProperties (which is hidden on Android), so I used an if statement on the WifiConfiguration(=configuredNetwork):
if(configuredNetwork.allowedKeyManagement.cardinality()==1)
wifiManager.removeNetwork(configuredNetwork.networkId);
wifiManager.saveConfiguration();
break;
And for some reason it worked. If anyone have an idea whats cardinality stands for or have a better way of doing this please share.

Android detect no data connection

I know there are a few threads on this topic but non of them seem to answer my question. I want to be able to detect when the device has no data connection. I have tried the NetworkInfo route checking if its null, isConnected(), isAvailable() etc. The problem im having is that these work fine (return false or null) when wifi or mobile network are disabled however when they are enabled but there is no signal they return true. Is there any way to detect no data connection due to no signal?
build reciver and catch broadcast
Intent action for network events in android sdk
You could try pinging google.com. If there's no response, you can be fairly sure that there's no connection (unless the user is in a country where it is blocked).
I use ConnectivityManager.getActiveNetworkInfo.isConnectedOrConnecting()
I just tested it 5 minutes ago to make sure, and if everything is working and I then walk away from my house until my router is no longer within range and the network strength indicator on the status bar shows no network, a call to isConnectedOrConnecting() from within an app returns false.
I take it that's not the behavior you are seeing?

Upload data when Wi-Fi is connected?

I'm making an app that is essentially a web form. I fill it out and send it to a website to be processed. But I only want it to send the data when connected to wifi.
I was thinking of putting the data into a tinyDB then running a check for wifi immediately. If connected it would submit the form and delete the db entry. I'd probably also run a check when the app is loaded and closed. It's important that I don't lose the data.
Is there a better way to do this?
if the form is small, you may not even need a db, you could just use SharedPreferences
http://developer.android.com/guide/topics/data/data-storage.html#pref , unless you have multiple rows of the user answering again and again then this might not be the best solution
here's some network check code if you need it
ConnectivityManager manager =(ConnectivityManager)activityOrContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if(info==null || !info.isConnected()){
return false;// network not active
}
not sure if you need this, you may already know how to do this or have it figured out
Have a look at my question How to respect network use settings in Android. Some of the answers demonstrate how to check the network type.
You can prompt the user to ask him if he wants to enable Wifi, and then you can enable it from code. Take a look at WifiManager class.

Categories

Resources