开发者

Check Internet connection problem in android?

开发者 https://www.devze.com 2023-03-18 20:00 出处:网络
Today I found a strange thing whil开发者_开发知识库e testing my application in android device. In My Device I have enable the WIFI connection, but due to some internal problem internet connectivity is

Today I found a strange thing whil开发者_开发知识库e testing my application in android device. In My Device I have enable the WIFI connection, but due to some internal problem internet connectivity is not available, and my application was not executed and after restarting the device it works fine. So how to handle this situation.

[In my application I have validate WIFI and Mobile Network]


I had a weird problem on the network in work. I would connect phone to our network over wifi and it would connect to the network grand and the status would be "Connected to name of network". But after about 10 minutes off my phone being connected to Wifi. When I check the phone status it says "authenticating with [name of network]. This problem resulted in a while of debugging as even when it says it is "authenticating" I could still access my gmail/facebook and websites on my phone. However when I tried my connect method similar to scorpio it would return not connected to internet. Here is mine:

 /**
 * Method to see if device has any access to the Internet. 
 * @return boolean true if connected, otherwise false.
 */
public boolean isConnectedToInternet()
{
    try{
        ConnectivityManager cm = (ConnectivityManager) getSystemService
                                                    (Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();

        if (netInfo != null && netInfo.isConnected())
        {
            //Network is available but check if we can get access from the network.
            URL url = new URL("http://www.Google.com/");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(2000); // Timeout 2 seconds.
            urlc.connect();

            if (urlc.getResponseCode() == 200)  //Successful response.
            {
                return true;
            } 
            else 
            {
                 Toast.makeText(this, "No connection to internet.", Toast.LENGTH_LONG).show();
                 Log.d("NO INTERNET", "NO INTERNET");
                return false;
            }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    Toast.makeText(this, "No connection to internet.", Toast.LENGTH_LONG).show();
    return false;
}

However when the phone saids its authenticating this line here would return null.

NetworkInfo netInfo = cm.getActiveNetworkInfo();

So like I said when authenicating I could still access web on phone and apps such as facebook/gmail still worked but apps such as mine or some of my colleagues who used a similar approach above didn't. Which obviously I can see why when I figured out why this was the case. Just got me curious to how are the apps such as facebook/gmail testing a connection to the internet. So I was wondering should I just take a simple approach of doing this:

URL url = new URL("http://www.Google.com/");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(2000); // Timeout 2 seconds.
            urlc.connect();

            if (urlc.getResponseCode() == 200)  //Successful response.
            {
                return true;
            } 
            else 
            {
                 Toast.makeText(this, "No connection to internet.", Toast.LENGTH_LONG).show();
                 Log.d("NO INTERNET", "NO INTERNET");
                return false;
            }

Anyway the question posted is rather vague so hard to say what the internal problem you speak off is and how to fix it. Just seemed similar to a fustration.


Try this,

protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager)     getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
    return true;
} else {
    return false;
}
}

OR

public boolean isOnline() {
 ConnectivityManager cm = (ConnectivityManager)     getSystemService(Context.CONNECTIVITY_SERVICE);
 return cm.getActiveNetworkInfo().isConnectedOrConnecting();

}
0

精彩评论

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