开发者

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

开发者 https://www.devze.com 2023-03-20 20:15 出处:网络
I have check for internet connectivity that goes like this: public static boolean isInternetAvailable(Context ctx)

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消