2

So, my question is how to check if I am receiving any data or not. The scenario is that I am connected to a wifi network such as Starbucks wifi (which user should first connect to the network and then accept the agreement before receiving any data.) This code is not serving my purpose.

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

It returns true when I am connected to the network but I have not accepted the agreement yet. Thanks,

Saeid Farivar
  • 1,667
  • 24
  • 43

4 Answers4

0

There's no way to know that, because that isn't something handled at an OS level- its handled at the router. THe OS only knows whether it has completed the handshake with the router and received an IP. Whether the router is going to throw him to a login screen is beyond the scope of the OS.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks for the answer, I guess saying that there is no way is not really accurate. I guess if I try connecting to a website like Google I can check if there is any data coming or not. is that right? I am trying not to implement my own logic though. – Saeid Farivar May 08 '13 at 18:13
  • No, you'd get a response either way. Those things are generally implemented by redirecting all traffic to a website. You could send a request to your own website which will return a fixed response, and compare the data returned. But that's a good amount of work, are you sure you get enough utility from this feature to go there? – Gabe Sechan May 08 '13 at 18:16
0

YOu can check this by trying to ping a website. For example

        try {
            Socket s = new Socket("www.google.com", 80);
            return s.getLocalAddress().getHostAddress();
                            // network connection available
        } catch (Exception e) {
            // no network connection
        }

I haven't tried it thogh.. But I think it should work.

If it doesn't work then you can try another thing. Send a httprequest to google and wait for response.

stinepike
  • 54,068
  • 14
  • 92
  • 112
0

My app connects to a bunch of web services running on a web server on the cloud. So I need to make sure this connection is established before my app tries to do stuff. Instead of trying to check if internet is available, all I do is call one of my services. I have a simple one called TestService that just returns the word "success". So my app calls the service, receives the string "success" and knows that it is connected to the internet and successfully reaching my services. I don't have to worry about ConnectivityManager and NetworkInfo because this solution works well.

Of course, if my services themselves were to go down, then I would not know for sure if the device is connecting to the internet, I would only know for sure it is not reaching my service. However a) I make sure my services are redundant and have 99.9% uptime, and b) my app wouldn't be much use if not connecting to my services anyway. An alternative to just check for internet might be to try to connect to a public service from a reputable company that you know will alyways be online.

Gary
  • 1,735
  • 4
  • 19
  • 36
0

Heres what I have run in an asynctask:

public Boolean testServer(){
    //stackoverflow.com/questions/9552743/proper-way-to-test-if-server-is-up-in-java    
    //stackoverflow.com/questions/9180072/android-check-connection-to-server
    Boolean boolconnect = false; 

    try {
          myUrl = new URL(UPLOAD_URL);
          connection = myUrl.openConnection();
          connection.setConnectTimeout(30*1000);
          connection.connect();
          Log.i(TAG, "FROM SERVER Connection made to: " + UPLOAD_URL);
          boolconnect = true;

    } catch (SocketException e) {
        // TODO Auto-generated catch block
        Log.i(TAG, "FROM SERVER socket exception:" + e);
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        Log.i(TAG, "FROM SERVER Unknown host:" + e);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.i(TAG, "FROM SERVER IOexception:" + e);
    }
    return boolconnect;
}
Stagleton
  • 1,060
  • 3
  • 11
  • 35
  • you're going to have to elaborate – Stagleton May 11 '13 at 19:20
  • What is not clear? Thanks for the code but when I tried the code, the return value was still true before accepting the agreement which should have been false. – Saeid Farivar May 11 '13 at 22:16
  • ok, download a string from a server and compare it with what the string should be. I guess you do have a connection but it's limited. http://stackoverflow.com/questions/2922210/reading-text-file-from-server-on-android – Stagleton May 12 '13 at 09:07