0

I use this code to check Internet connection, and my application can't seem to access the internet from the emulator.

public boolean isConnectedToInternet() {
    Runtime runtime = Runtime.getRuntime();
    try {
        Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
        int     exitValue = ipProcess.waitFor();
        return (exitValue == 0);
    } catch (IOException e)          { e.printStackTrace(); }
    catch (InterruptedException e) { e.printStackTrace(); }
    return false;
}

@Override 
protected void onCreate(Bundle savedInstanceState){
    ...
    if(isConnectedToInternet()) {
        util.toast(authentication.this, "connected to internet");
    } else {
        util.toast(authentication.this, "not connected to internet");
    }
}

Other internet access from the machine is working fine, and ping 8.8.8.8 from cmd gets replies.

Is there any way to find out what exactly is the problem?

  • Are you asking that why in your emulator internet connection is not shown ? – Aditya Feb 11 '18 at 16:44
  • `My application can't seem to access the internet from the emulator.` Then change the subject of your post please as now it does not make sense. – greenapps Feb 11 '18 at 16:54
  • Did you try access the Internet using web browser of your emulator? – nhoxbypass Feb 11 '18 at 17:00
  • @nhoxbypass Thanks, the web browser is working, so this confirms the problem is elsewhere. But old backups of my app that I know worked, also can't access the internet now. –  Feb 11 '18 at 17:06
  • @safebookverified did you ask INTERNET permission? – nhoxbypass Feb 11 '18 at 17:09
  • @nhoxbypass No, because I heard you don't need it any more. When I add INTERNET and ACCESS_NETWORK_STATE permissions, I still can't access internet through my application –  Feb 11 '18 at 17:15
  • @nhoxbypass I have daily backups of my application since 1 month ago. When I try the old backups it also can't access internet, so I think the application can't be the problem. –  Feb 11 '18 at 17:16
  • Can you try my command instead `runtime.exec("ping -c 1 -w 2 google.com")` to check where is the problem, this command worked on my emulator. And please add permissions to make sure it's not the problem! – nhoxbypass Feb 11 '18 at 17:17
  • @nhoxbypass I added those 2 permissions but it doesn't fix the problem. And I changed that line to Process ipProcess = runtime.exec("ping -c 1 -w 2 google.com"); but it still says "not connected to internet". –  Feb 11 '18 at 17:22
  • Please don't repeat questions. Simply editing this post with any new information you had, or any new code you'd tried would've bumped it to the top of the active queue. I've closed this as a duplicate of the newer one, since you got your answer there, but, in the future, please just edit the original. – Mike M. Feb 12 '18 at 09:48

0 Answers0