-2

I had been working on a app for my college project. It that application i just want to check if a website is available(online) or not. If it is available then open it in webview and if it isn't open a pre specified website.

After some research I landed up till the following code but it does not seem to work. App always opens bing.com (i.e value of flag does not get updated after running pingHost)

public class MainActivity extends Activity {

    WebView web1;
    String Address;
    int flag=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Timer repeatTask = new Timer();
        repeatTask.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        pingHost("http://www.google.com", 80, 5000);
                        if (flag==1) {
                            web1 = (WebView) findViewById(R.id.webView1);
                            Address = "https://learn2lead.home.blog";
                            WebSettings webSetting = web1.getSettings();
                            webSetting.setBuiltInZoomControls(true);
                            webSetting.setJavaScriptEnabled(true);
                            web1.setWebViewClient(new WebViewClient());
                            web1.loadUrl(Address);
                        } else if (flag==0){
                            web1 = (WebView) findViewById(R.id.webView1);
                            Address = "http://bing.com";
                            WebSettings webSetting = web1.getSettings();
                            webSetting.setBuiltInZoomControls(true);
                            webSetting.setJavaScriptEnabled(true);
                            web1.setWebViewClient(new WebViewClient());
                            web1.loadUrl(Address);
                        }
                    }
                });
            }
        }, 0, 10000);

    public void pingHost(final String host, final int port, final int timeout) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try (Socket socket = new Socket()) {
                    socket.connect(new InetSocketAddress(host, port), timeout);
                    flag = 1;
                } catch (IOException e) {
                    flag = 0;
                }
            }


        }).start();
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Try

public class MainActivity extends Activity {

WebView web1;
String Address;
int flag = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Timer repeatTask = new Timer();
    repeatTask.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            if (isInternetAvailable()){
                flag = 1;
            }else{
                flag = 0;
            }
            System.out.println("pingHost flag: " + flag );
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (flag == 1) {
                        web1 = (WebView) findViewById(R.id.webView1);
                        Address = "https://learn2lead.home.blog";
                        WebSettings webSetting = web1.getSettings();
                        webSetting.setBuiltInZoomControls(true);
                        webSetting.setJavaScriptEnabled(true);
                        web1.setWebViewClient(new WebViewClient());
                        web1.loadUrl(Address);
                    } else if (flag == 0) {
                        web1 = (WebView) findViewById(R.id.webView1);
                        Address = "http://bing.com";
                        WebSettings webSetting = web1.getSettings();
                        webSetting.setBuiltInZoomControls(true);
                        webSetting.setJavaScriptEnabled(true);
                        web1.setWebViewClient(new WebViewClient());
                        web1.loadUrl(Address);
                    }
                }
            });
        }
    }, 0, 10000);
}

public boolean isInternetAvailable() {
    try {
        InetAddress ipAddr = InetAddress.getByName("google.com");
        //You can replace it with your name
        return !ipAddr.equals("");

    } catch (Exception e) {
        return false;
    }
}
}

Modified your posted question to working.

But implementing you should consider about that timer task close and think it really nessary 10sec period.


to check whether internet available this is much reliable and fast

public boolean isInternetAvailable() {
        try {
            int timeoutMs = 1500;
            Socket sock = new Socket();
            SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

            sock.connect(sockaddr, timeoutMs);
            sock.close();

            return true;
        } catch (IOException e) { return false; }
    }

check : https://stackoverflow.com/a/27312494/1025070

UdayaLakmal
  • 4,035
  • 4
  • 29
  • 40
  • The InetAddress class has a cache to store successful as well as unsuccessful host name resolutions. – Scary Wombat Apr 09 '19 at 07:00
  • @ScaryWombat you are right. Please refer https://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddre and check accepted ansewer. – UdayaLakmal Apr 09 '19 at 07:25
  • 1
    OMG! Worked like charm... I spent two days scraching my head and thinking about it. Thank you so much... @UdayaLakmal – Pranav Gawade Apr 09 '19 at 13:13