1

I need some method to verify if my network is conected to interne, and during my researchs I found some method describe below:

public static boolean hasInternetAccess(Context context) {
    if (isNetworkAvailable(context)) {
        try {
            HttpURLConnection urlc = (HttpURLConnection) 
                (new URL("http://clients3.google.com/generate_204")
                .openConnection());
            urlc.setRequestProperty("User-Agent", "Android");
            urlc.setRequestProperty("Connection", "close");
            urlc.setConnectTimeout(1500); 
            urlc.connect();
            return (urlc.getResponseCode() == 204 &&
                        urlc.getContentLength() == 0);
        } catch (IOException e) {
            Log.e(TAG, "Error checking internet connection", e);
        }
    } else {
        Log.d(TAG, "No network available!");
    }
    return false;
}

...but my programs will be distributed on China, and Google is blocked on China.

I have on mind to create a method like this selecting first my localization to get a Baidoo web site - like Google on China - but make this is very expansive method to check if device is connected on some network and verify if than connected on Internet.

User0123456789
  • 760
  • 2
  • 10
  • 25
  • Send a HEAD request to any page and see if you get a response. Or try using ping. – tynn May 05 '16 at 18:00
  • For China probably the easiest would be something like `InetAddress.getByName("baidu.com").isReachable(TIMEOUT_IN_MILLISECONDS);` and check if it throws an exception. – ccpizza Apr 07 '17 at 17:29

2 Answers2

1

You could try something like this using ConnectivityManager :

public static boolean  isConnectedToInternet(Context context){
        boolean isConnected= false;
        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mobile = connectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);        
        isConnected= (wifi.isAvailable() && wifi.isConnectedOrConnecting() || (mobile.isAvailable() && mobile.isConnectedOrConnecting()));
        return isConnected;
    }

You can also check the selected answers here and here

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • ishmaelMakitla its not answer He expected please read question – Maheshwar Ligade May 05 '16 at 18:05
  • Maheshwar Ligade, I checked - I thought he said "sending HTTP request" is expensive and wanted a way of checking if there's Internet/data connectivity. I see in your suggested answer you are sending a HTTP-GET to Yahoo - whereas in my case I am using connectivity manager to check if the device itself is connected to Internet without making a request. Did I perhaps miss something? Thanks for the comment. – ishmaelMakitla May 05 '16 at 18:10
  • " Verify if device is connected with internet," He want this one not network connected or not which is given by your code – Maheshwar Ligade May 05 '16 at 18:17
  • This code only verify if Wifi is enable or disable, and not if exists some internet connection. I use a similar to verify my wifi enable or disable. – COELHO G. C. May 12 '16 at 18:32
0

China does have sites to host android apps. China has proxy servers you could use to see what is blocked and could be used to thwart whatever firewall China has. I strongly encourage you to consider legal ramifications. TCP/IP utilities or even a browser can be used to test a connection. I am not sure I fully understand your question. You have Android apps that will be used in devices located in China and a server located somewhere you want to test a connection. China has web hosting if you want to go that route. Do you need access to Google API's?

Pomagranite
  • 696
  • 5
  • 11
  • No, I don't need access Google API's yet, but I have more of one servers, and a service redirecting to more faster server. One of this servers is localized on China, but Chineze server is not a better and sometimes his is broken. When this happening the software try conneting others servers but, just because proxy and firewalls, the redirecting flow slowly and timeout problems occured frequently! – COELHO G. C. May 12 '16 at 18:46